Printf
Printf モジュールは、C 標準ライブラリの printf と同様の書式付き出力関数を提供します。出力ストリームまたは文字列への書式付き出力が可能です。
Printf.@printf — Macro
@printf([io::IO], "%Fmt", args...)C argsスタイルの書式指定文字列を使って、printfを出力します。出力先を変更する場合は、オプションでIOを第1引数として渡せます。
Examples
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書式の体系的な仕様については、hereを参照してください。また、出力する代わりに結果をStringとして取得するには、@sprintfを参照してください。
Caveats
フラグ%a、%A、%e、%E、%f、%F、%g、%Gでは、InfとNaNは、それぞれ一貫してInfとNaNとして出力されます。さらに、浮動小数点数が2つの出力文字列の数値に同じだけ近い場合は、ゼロから遠い方の出力文字列が選ばれます。
Examples
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.007812Julia 1.8以降では、%s(文字列)と%c(文字)の幅はtextwidthを使って計算されます。たとえば、幅がゼロの文字(発音区別符号を表す結合文字など)は無視され、特定の「全角」文字(絵文字など)は幅2として扱われます。
Printf.@sprintf — Macro
@sprintf("%Fmt", args...)@printf を文字列としてフォーマットした出力を返します。
Examples
julia> @sprintf "this is a %s %15.1f" "test" 34.567
"this is a test 34.6"Printf.Format — Type
Printf.Format(format_str)値の書式設定に使用できる、C の printf 互換フォーマットオブジェクトを作成します。
入力の format_str には、有効な書式指定文字と修飾子を任意に指定できます。
Format オブジェクトを Printf.format(f::Format, args...) に渡すと書式化された文字列を生成でき、Printf.format(io::IO, f::Format, args...) に渡すと書式化された文字列を直接 io に出力できます。
簡便な方法として、Printf.format"..." 文字列マクロ形式を使うと、マクロ展開時に Printf.Format オブジェクトを作成できます。
Printf.format — Function
Printf.format(f::Printf.Format, args...) => String
Printf.format(io::IO, f::Printf.Format, args...)printf形式のオブジェクトfを、指定されたargsに適用してフォーマット済み文字列を返す(第1の方法)か、ioオブジェクトに直接出力します(第2の方法)。Cのprintfサポートの詳細については、@printfを参照してください。