Printf
Printf.@printf — 宏@printf([io::IO], "%Fmt", args...)使用 C printf 风格的格式说明字符串打印 args。可以选择将 IO 作为第一个参数传递以重定向输出。
示例
julia> @printf "Hello %s" "world"
Hello world
julia> @printf "Scientific notation %e" 1.234
Scientific notation 1.234000e+00
julia> @printf "Scientific notation three digits %.3e" 1.23456
Scientific notation three digits 1.235e+00
julia> @printf "Decimal two digits %.2f" 1.23456
Decimal two digits 1.23
julia> @printf "Padded to length 5 %5i" 123
Padded to length 5 123
julia> @printf "Padded with zeros to length 6 %06i" 123
Padded with zeros to length 6 000123
julia> @printf "Use shorter of decimal or scientific %g %g" 1.23 12300000.0
Use shorter of decimal or scientific 1.23 1.23e+07
julia> @printf "Use dynamic width and precision %*.*f" 10 2 0.12345
Use dynamic width and precision 0.12有关格式的系统规范,请参见 此处。另请参见 @sprintf 以获取结果作为 String,而不是打印。
注意事项
对于标志 %a、%A、%e、%E、%f、%F、%g 和 %G,Inf 和 NaN 始终打印为 Inf 和 NaN。此外,如果浮点数与两个可能的输出字符串的数值等距,则选择远离零的输出字符串。
示例
julia> @printf("%f %F %f %F", Inf, Inf, NaN, NaN)
Inf Inf NaN NaN
julia> @printf "%.0f %.1f %f" 0.5 0.025 -0.0078125
0 0.0 -0.007812从 Julia 1.8 开始,%s (字符串) 和 %c (字符) 的宽度使用 textwidth 计算,例如忽略零宽度字符 (例如用于变音符号的组合字符) 并将某些“宽”字符 (例如表情符号) 视为宽度 2。
动态宽度说明符 (例如 %*s 和 %0*.*f) 需要 Julia 1.10。
Printf.@sprintf — 宏@sprintf("%Fmt", args...)将 @printf 格式化的输出作为字符串返回。
示例
julia> @sprintf "this is a %s %15.1f" "test" 34.567
"this is a test 34.6"