ThePrintf
module provides formatted output functions similar to the C standard library'sprintf
. It allows formatted printing to an output stream or to a string.
Printf.@printf
—Macro@printf([io::IO], "%Fmt", args...)
Printargs
using Cprintf
style format specification string. Optionally, anIO
may be passed as the first argument to redirect output.
Examples
julia> @printf "Hello %s" "world"Hello worldjulia> @printf "Scientific notation %e" 1.234Scientific notation 1.234000e+00julia> @printf "Scientific notation three digits %.3e" 1.23456Scientific notation three digits 1.235e+00julia> @printf "Decimal two digits %.2f" 1.23456Decimal two digits 1.23julia> @printf "Padded to length 5 %5i" 123Padded to length 5 123julia> @printf "Padded with zeros to length 6 %06i" 123Padded with zeros to length 6 000123julia> @printf "Use shorter of decimal or scientific %g %g" 1.23 12300000.0Use shorter of decimal or scientific 1.23 1.23e+07julia> @printf "Use dynamic width and precision %*.*f" 10 2 0.12345Use dynamic width and precision 0.12
For a systematic specification of the format, seehere. See also@sprintf
to get the result as aString
instead of it being printed.
Caveats
Inf
andNaN
are printed consistently asInf
andNaN
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
julia> @printf("%f %F %f %F", Inf, Inf, NaN, NaN)Inf Inf NaN NaNjulia> @printf "%.0f %.1f %f" 0.5 0.025 -0.00781250 0.0 -0.007812
Starting in Julia 1.8,%s
(string) and%c
(character) widths are computed usingtextwidth
, which e.g. ignores zero-width characters (such as combining characters for diacritical marks) and treats certain "wide" characters (e.g. emoji) as width2
.
Dynamic width specifiers like%*s
and%0*.*f
require Julia 1.10.
Printf.@sprintf
—Macro@sprintf("%Fmt", args...)
Return@printf
formatted output as string.
Examples
julia> @sprintf "this is a %s %15.1f" "test" 34.567"this is a test 34.6"
Printf.Format
—TypePrintf.Format(format_str)
Create a C printf-compatible format object that can be used for formatting values.
The inputformat_str
can include any valid format specifier character and modifiers.
AFormat
object can be passed toPrintf.format(f::Format, args...)
to produce a formatted string, orPrintf.format(io::IO, f::Format, args...)
to print the formatted string directly toio
.
For convenience, thePrintf.format"..."
string macro form can be used for building aPrintf.Format
object at macro-expansion-time.
Printf.Format
requires Julia 1.6 or later.
Printf.format
—FunctionPrintf.format(f::Printf.Format, args...) => StringPrintf.format(io::IO, f::Printf.Format, args...)
Apply a printf format objectf
to providedargs
and return the formatted string (1st method), or print directly to anio
object (2nd method). See@printf
for more details on Cprintf
support.
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.