C# Programming

Note

文字列のフォーマット

開発環境: Visual Studio 2003

目次

  1. 目次
  2. Formatting String
  3. Picture Formatting
  4. サンプルコード
  5. Formattingの実行結果
  6. 改訂履歴

Formatting String


FormatsFormat CharacterExample
通貨
c/C
123 → -\123
十進数d/D 123 → -123
d6/D6123 → -000123
浮動小数点e 123.456 → -1.234560e+002
E 123.456 → -1.234560E+002
E3 123.456 → -1.235E+002
固定小数点f/F 123.456 → -123.46
f5/F5 123.456 → -123.45600
一般g/G Hello World → Hello World
g/G 123.456 → -123.456
g2/G2 Hello World → Hello World
g2/G2 123.456 → -1.2e+02
g 123.456 → -1.23456e-08
G 123.456 → -1.23456E-08
長い日付、時間F 2002年3月21日 1:09:03
短い日付、時間g2002/04/01 13:01
gg西暦
gggg西暦
数値n/N123456.789 → 123,456.79
n5/N5123456.789 → 123,456.78912
パーセントp/P 123.456 → 12,345.60%
p5/P5123.456 → 12,345.60000%
十六進数x255 → ff
xx255 → FF
x16X16 255 → 00000000000000FF

Picture Formatting


FormatsFormat CharacterExample
Display zero placeholder
0
3.1429の場合、
"000.##"→003.14
"0###.##"→0003.14
"000.###"→003.143
Display digit placeholder#
Decimal point.
Group separator and Multiplier,
Percent notation%"%#.##"
Exponent notationE+0000.##E-00
000.##E+00
000.##e-00
000.##e+00
E-0

e+0

e-0

Literal character{

}

"{{##.###}}"→{3,14}
Literal string'ABC'

"ABC"

Section separator;正、負、0の場合の出力を指定できる。
OtherAll other characters

サンプルコード

using System;

namespace TestString
{
        /// <summary>
        /// Stringのテストです。 
        /// </summary>
        class TestString
        {
                static void Main(string[] args)
                {
                        // String Format
                        Console.WriteLine("{0} = {0},  {1} != {2}", 1, 2, 3);
                        
                        Console.WriteLine("\nCurrency format");
                        Console.WriteLine("c/C\t123 = {0:c}", -123);

                        Console.WriteLine("\nDecimal format");
                        Console.WriteLine("d/D\t123 = {0:d}", -123);
                        Console.WriteLine("d6/D6\t123 = {0:d6}", -123);

                        Console.WriteLine("\nScientific (exponential) format");
                        Console.WriteLine("e\t123.456 = {0:e}", -123.456);
                        Console.WriteLine("E\t123.456 = {0:E}", -123.456);
                        Console.WriteLine("E3\t123.456 = {0:E3}", -123.456);

                        Console.WriteLine("\nFixed-point format");
                        Console.WriteLine("f/F\t123.456 = {0:f}", -123.456);
                        Console.WriteLine("f5/F5\t123.456 = {0:f5}", -123.456);
                        
                        Console.WriteLine("\nGeneral format");
                        Console.WriteLine("g/G\tHello World = {0:g}", "Hello World");
                        Console.WriteLine("g/G\t123.456 = {0:g}", -123.456);
                        Console.WriteLine("g2/G2\tHello World = {0:g2}", "Hello World");
                        Console.WriteLine("g2/G2\t123.456 = {0:g2}", -123.456);
                        Console.WriteLine("g\t123.456 = {0:g}", -123.456e-10);
                        Console.WriteLine("G\t123.456 = {0:G}", -123.456e-10);
                        
                        Console.WriteLine("\nNumber format");
                        Console.WriteLine("n/N\t123456.789 = {0:n}", 123456.789);
                        Console.WriteLine("n5/N5\t123456.789 = {0:n5}", 123456.789123);
                        
                        Console.WriteLine("\nPercent format");
                        Console.WriteLine("p/P\t123.456 = {0:p}", 123.456);
                        Console.WriteLine("p5/P5\t123.456 = {0:p5}", 123.456);
                        
                        Console.WriteLine("\nHexadecimal format");
                        Console.WriteLine("x\t255 = {0:x}", 255);
                        Console.WriteLine("X\t255 = {0:X}", 255);
                        Console.WriteLine("X16\t255 = {0:X16}", 255);

                        Console.WriteLine("\nplaceholder");
                        Console.WriteLine("0###.##\t{0} = {0:0###.##}", Math.PI);
                        Console.WriteLine("0##.###\t{0} = {0:0##.###}", Math.PI);
                        Console.WriteLine("000.##\t{0} = {0:000.##}", Math.PI);
                        Console.WriteLine("%000.##\t{0} = {0:%000.##}", Math.PI);
                        Console.WriteLine("000.##E-00\t{0} = {0:000.##E-00}", Math.PI);
                        Console.WriteLine("000.##E+00\t{0} = {0:000.##E+000}", Math.PI);
                        Console.WriteLine("000.##e-00\t{0} = {0:000.##e-00}", Math.PI);
                        Console.WriteLine("000.##e+00\t{0} = {0:000.##e+0000}", Math.PI);

                        Console.WriteLine("\ncurly brackets and literal string ");
                        Console.WriteLine("0###.##\t{0} = {{{0:0###.##}}'literal string'}", Math.PI);

                        Console.WriteLine("\nSection separator");
                        Console.WriteLine( "{0:Positive Number = #.##;Negative Number = -#.##;Zero Value = #}", Math.PI);
                        Console.WriteLine( "{0:Positive Number = #;Negative Number = -#;Zero Value = #}", -Math.PI);
                        Console.WriteLine( "{0:Positive Number = #;Negative Number = -#;Zero Value = #}", 0);
                }
        }
}

Formattingの実行結果

1 = 1,  2 != 3

Currency format
c/C     123 = -\123

Decimal format
d/D     123 = -123
d6/D6   123 = -000123

Scientific (exponential) format
e       123.456 = -1.234560e+002
E       123.456 = -1.234560E+002
E3      123.456 = -1.235E+002

Fixed-point format
f/F     123.456 = -123.46
f5/F5   123.456 = -123.45600

General format
g/G     Hello World = Hello World
g/G     123.456 = -123.456
g2/G2   Hello World = Hello World
g2/G2   123.456 = -1.2e+02
g       123.456 = -1.23456e-08
G       123.456 = -1.23456E-08

Number format
n/N     123456.789 = 123,456.79
n5/N5   123456.789 = 123,456.78912

Percent format
p/P     123.456 = 12,345.60%
p5/P5   123.456 = 12,345.60000%

Hexadecimal format
x       255 = ff
X       255 = FF
X16     255 = 00000000000000FF

placeholder
0###.## 3.14159265358979 = 0003.14
0##.### 3.14159265358979 = 003.142
000.##  3.14159265358979 = 003.14
%000.## 3.14159265358979 = %314.16
000.##E-00      3.14159265358979 = 314.16E-02
000.##E+00      3.14159265358979 = 314.16E-002
000.##e-00      3.14159265358979 = 314.16e-02
000.##e+00      3.14159265358979 = 314.16e-0002

curly brackets and literal string
0###.## 3.14159265358979 = {0003.14}literal string

Section separator
Positive Number = 3.14
Negative Number = -3
Zero Value =

改訂履歴

2003/3/30 Stringの比較で <, > 演算子が使えるという間違いを修正。BBS No.112 スレッド以下を参照。