13,005
回編集
144行目: | 144行目: | ||
|} | |} | ||
</center> | </center> | ||
<br> | |||
<source lang="c++"> | |||
namespace Format | |||
{ | |||
class Program | |||
{ | |||
static void Main(string[] args) | |||
{ | |||
// c(C) : 通貨 | |||
Console.WriteLine("{0:c}", 100); // \100 | |||
Console.WriteLine("{0:c}", 200.0); // \200 | |||
// d(D) : 10進数表示(整数型のみ) | |||
Console.WriteLine("{0:d}", 100); // 100 | |||
Console.WriteLine("{0:d6}", 100); // 000100 6桁表記 | |||
Console.WriteLine("{0:d99}", 100); // 0...0100 99桁表記 | |||
Console.WriteLine("{0:d100}", 100); // d1100 | |||
// 精度指定子は0 - 99までなので以下は不適切 | |||
// d1と00(カスタム数値書式指定子の0)の組み合わせと解釈される | |||
// e(E) : 浮動小数点 | |||
// 標準では小数点以下は6桁まで表示される | |||
Console.WriteLine("{0:e}", 1234); // 1.234000e+003 | |||
Console.WriteLine("{0:e}", 123456789); // 1.234568e+008 | |||
Console.WriteLine("{0:E}", 123456789); // 1.234568E+008 | |||
Console.WriteLine("{0:e10}", 123456789); // 1.2345678900e+008 | |||
Console.WriteLine("{0:e10}", 1.23456789); // 1.2345678900e+000 | |||
Console.WriteLine("{0:e10}", 12345.67891234567); // 1.2345678912e+004 | |||
// f(F) : 固定小数点 | |||
// 標準では小数点以下は2桁まで表示される | |||
Console.WriteLine("{0:f}", 1234); // 1234.00 | |||
Console.WriteLine("{0:f}", 1.23456789012345); // 1.23 | |||
Console.WriteLine("{0:f}", 12345.67891234567); // 12345.68 | |||
Console.WriteLine("{0:f10}", 1.23456789012345); // 1.2345678901 | |||
Console.WriteLine("{0:f10}", 12345.67891234567); // 12345.6789123457 | |||
// g(G) : 浮動小数点か固定小数点のうち最適な方を自動で選択されて表示される | |||
// 0.0001未満の値を扱う場合や精度指定子から数値があふれている場合、浮動小数点による表記 | |||
Console.WriteLine("{0:g}", 123456789.012345); // 123456789.012345 固定小数点 | |||
Console.WriteLine("{0:g5}", 123456789.012345); // 1.2346e+08 浮動小数点 | |||
Console.WriteLine("{0:g}", 0.0001); // 0.0001 固定小数点 | |||
Console.WriteLine("{0:g}", 0.000099); // 9.9e-05 浮動小数点 | |||
// n(N) : 3桁間隔で区切る | |||
// 標準では小数点以下は2桁までのみ表示される | |||
Console.WriteLine("{0:n}", 1234); // 1,234.00 | |||
Console.WriteLine("{0:n}", 123456789); // 123,456,789.00 | |||
Console.WriteLine("{0:n}", 1.23456789012345); // 1.23 | |||
Console.WriteLine("{0:n}", 123456789.1234); // 123,456,789.12 | |||
Console.WriteLine("{0:n4}", 123456789.1234); // 123,456,789.1234 | |||
// p(P) : パーセント表示 | |||
// 標準では%表記で小数点2桁までのみ表示される | |||
Console.WriteLine("{0:p}", 0.12); // 12.00% | |||
Console.WriteLine("{0:p}", 0.123456); // 12.35% | |||
Console.WriteLine("{0:p}", 1.2345); // 123.45% | |||
Console.WriteLine("{0:p4}", 0.123456789); // 12.3457% 小数点4桁まで | |||
// r(R) : ラウンドトリップ書式指定子 | |||
Console.WriteLine("{0:r}", 12.34567); // 12.34567 | |||
Console.WriteLine("{0:r}", 12.34567890123456789f); // 12.345679 flaot型は9桁のみ表示される | |||
Console.WriteLine("{0:r15}", 12.34567890123456789f); // 12.345679 同様にflaot型なので9桁のみ表示される | |||
Console.WriteLine("{0:r}", 12.34567890123456789); // 12.345678901234567 double型は17桁のみ表示される | |||
//Console.WriteLine("{0:r}", 1234); // 整数はエラーが起きる | |||
// x(X) : 16進数表示 | |||
Console.WriteLine("{0:x}", 9); // 9 | |||
Console.WriteLine("{0:X}", 10); // A | |||
Console.WriteLine("{0:x}", 10); // a | |||
Console.WriteLine("{0:x}", 15); // f | |||
Console.WriteLine("{0:x}", 16); // 10 | |||
Console.WriteLine("{0:x}", 17); // 11 | |||
} | |||
} | |||
} | |||
</source> | |||
<br> | <br> | ||
'''精度指定子'''<br> | '''精度指定子'''<br> |