當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Julia 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

有關格式的係統規範,請參閱這裏。另見 @sprintf

注意事項

InfNaN 始終打印為 InfNaN 。對於標誌 %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

相關用法


注:本文由純淨天空篩選整理自julialang.org 大神的英文原創作品 Printf.@printf — Macro。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。