Formatting numbers and strings

The @printf macro from the Printf package (we'll look deeper into macros in Chapter 7, Metaprogramming in Julia) takes a format string and one or more variables to substitute into this string while being formatted. It works in a manner similar to printf in C. You can write a format string that includes placeholders for variables, for example, as follows:

julia> name = "Pascal" 
julia> using Printf 
julia> @printf("Hello, %s \n", name) # returns Hello, Pascal 

Because @printf now lives in another package, you have to do this using Printf first (prior to 1.0, it belonged to Base).

If you need a string as the return value, use the macro @sprintf.

The following formatting.jl script shows the most common formats: ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.