Printf
Printf
モジュールは、C標準ライブラリのprintf
に似たフォーマット出力関数を提供します。出力ストリームまたは文字列へのフォーマット印刷を可能にします。
Printf.@printf
— Macro@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
フォーマットの体系的な仕様については、こちらを参照してください。また、印刷されるのではなく String
として結果を取得するには @sprintf
を参照してください。
注意事項
Inf
と NaN
は、フラグ %a
、%A
、%e
、%E
、%f
、%F
、%g
、および %G
に対して一貫して Inf
と NaN
として印刷されます。さらに、浮動小数点数が2つの可能な出力文字列の数値値に等しく近い場合、ゼロから遠い出力文字列が選択されます。
例
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
— Macro@sprintf("%Fmt", args...)
文字列として@printf
形式の出力を返します。
例
julia> @sprintf "this is a %s %15.1f" "test" 34.567
"this is a test 34.6"
Printf.Format
— TypePrintf.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
はJulia 1.6以降が必要です。
Printf.format
— FunctionPrintf.format(f::Printf.Format, args...) => String
Printf.format(io::IO, f::Printf.Format, args...)
提供された args
に printf フォーマットオブジェクト f
を適用し、フォーマットされた文字列を返します(1つ目のメソッド)。または、io
オブジェクトに直接印刷します(2つ目のメソッド)。C printf
サポートの詳細については @printf
を参照してください。