Printf

Printf.@printf — Macro

  1. @printf([io::IO], "%Fmt", args...)

Print args using C printf style format specification string. Optionally, an IO may be passed as the first argument to redirect output.

Examples

  1. julia> @printf "Hello %s" "world"
  2. Hello world
  3. julia> @printf "Scientific notation %e" 1.234
  4. Scientific notation 1.234000e+00
  5. julia> @printf "Scientific notation three digits %.3e" 1.23456
  6. Scientific notation three digits 1.235e+00
  7. julia> @printf "Decimal two digits %.2f" 1.23456
  8. Decimal two digits 1.23
  9. julia> @printf "Padded to length 5 %5i" 123
  10. Padded to length 5 123
  11. julia> @printf "Padded with zeros to length 6 %06i" 123
  12. Padded with zeros to length 6 000123
  13. julia> @printf "Use shorter of decimal or scientific %g %g" 1.23 12300000.0
  14. Use shorter of decimal or scientific 1.23 1.23e+07

For a systematic specification of the format, see here. See also @sprintf.

Caveats

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.

Examples

  1. julia> @printf("%f %F %f %F", Inf, Inf, NaN, NaN)
  2. Inf Inf NaN NaN
  3. julia> @printf "%.0f %.1f %f" 0.5 0.025 -0.0078125
  4. 0 0.0 -0.007812

Julia 1.8

Starting in Julia 1.8, %s (string) and %c (character) widths are computed using textwidth, which e.g. ignores zero-width characters (such as combining characters for diacritical marks) and treats certain “wide” characters (e.g. emoji) as width 2.

Printf.@sprintf — Macro

  1. @sprintf("%Fmt", args...)

Return @printf formatted output as string.

Examples

  1. julia> @sprintf "this is a %s %15.1f" "test" 34.567
  2. "this is a test 34.6"