Caveats in Julia
Inf and NaN are printed consistently as Inf and NaN for flags %a, %A, %e, %E, %f, %F, %g, and %G. Furthermore, if a floating point number is equally close to the numeric values of two possible output strings, the output string further away from zero is chosen.
@printf("%f %F %f %F", Inf, Inf, NaN, NaN)
Output:
@printf "%.0f %.1f %f" 0.5 0.025 -0.0078125
Output:
How to use Printf in Julia?
In Julia, printf is not a built-in function but a macro provided by the Printf module of Julia’s standard library. This macro allows you to format strings similarly to the C programming language’s printf function. The Printf.@printf macro takes a format string followed by zero or more arguments and outputs a formatted string to the standard output.
Syntax:
@printf([io::IO], “%Fmt”, args…)
Print arguments using C style format specification string. Optionally, an IO may be passed as the first argument to redirect output.
Note:
- There is no requirement to import the Printf module in Julia like other modules.
- using Printf is necessary for using the @printf macro.
- Both the macros in the Printf module can also be used with a full path like “Printf.@printf()”.
- Ensure that ‘P’ in Printf is a capital letter and everything else is small.
First type the ‘using Printf’ function in Julia REPL so that we can use the @printf macro ahead: