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.

Julia
@printf("%f %F %f %F", Inf, Inf, NaN, NaN)

Output:

Julia
@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:

  1. There is no requirement to import the Printf module in Julia like other modules.
  2. using Printf is necessary for using the @printf macro.
  3. Both the macros in the Printf module can also be used with a full path like “Printf.@printf()”.
  4. 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:

using Printf in Julia REPL

Similar Reads

Format Specifiers in Julia

1. String Format Specifier %s...

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....

sprintf in Julia

Similarly, like the @printf macro of the Printf module, Julia also has an @sprintf macro in the Printf module. @sprintf macro is used to return the formatted output of @printf as a string....