AnyMath 文档

Printf打印

Printf打印 模块提供类似于C标准库的格式化输出函数 printf打印. 它允许格式化打印到输出流或字符串。

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

印刷业 阿格斯 使用C printf打印 样式格式规范字符串。 任选地,一个 伊俄 可以作为重定向输出的第一个参数传递。

*例子*

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

有关格式的系统规范,请参阅https://en.cppreference.com/w/c/io/fprintf[这里]。 @sprintf得到结果为 字符串 而不是被打印出来。

*注意事项*

资讯 被一致地打印为 资讯 旗帜 %a, %A, %e, %E, %f, %F, %g,而 %G. 此外,如果浮点数同样接近两个可能的输出字符串的数值,则选择远离零的输出字符串。

*例子*

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
兼容性

朱莉娅1.8从朱莉娅1.8开始, %s (字符串)和 %c (字符)宽度计算使用 文本宽度,例如忽略零宽度字符(例如为变音符号组合字符),并将某些"宽"字符(例如表情符号)视为宽度 2.

兼容性

Julia1.10动态宽度说明符,如 %*s%0*.*f 需要Julia1.10。

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

回来吧 @printf格式化输出为字符串。

*例子*

julia> @sprintf "this is a %s %15.1f" "test" 34.567
"this is a test            34.6"
Printf.Format(format_str)

创建可用于格式化值的c printf兼容format对象。

输入 格式_str 可以包括任何有效的格式说明符字符和修饰符。

A 格式 对象可以传递给 Printf。格式(f::Format,args...) 生成格式化字符串,或 Printf。格式(io::IO,f::Format,args...) 要将格式化的字符串直接打印到 伊俄.

为方便起见, Printf。格式"。.." 字符串宏形式可用于构建一个 Printf的。格式 对象在宏观-扩展-时间。

兼容性

朱莉娅1.6 Printf的。格式 需要Julia1.6或更高版本。

Printf.format(f::Printf.Format, args...) => String
Printf.format(io::IO, f::Printf.Format, args...)

应用printf格式对象 f 提供 阿格斯 并返回格式化的字符串(第一种方法),或直接打印到 伊俄 对象(第二种方法)。 见 @printf有关C的更多详情 printf打印 支持。