. net string formatting

Time:2020-7-30

summary:

When we convert a type (usually number and time) to a string, we can control the style of its display.

The basic writing method is to provide format parameters when calling toString or format methods. Since the specific parameters can’t be remembered, each time you use them, you have to search the Internet for a waste of time. Therefore, summarize them for your notes.

code:

           {

                double PI = 3.1415926535897932d;

                double e = 2.718281828459d;

            

                Console.WriteLine(PI.ToString());

                Console.WriteLine(PI.ToString(“#.##”));

                Console.WriteLine(string.Format(“{0:#.##}”, PI));

                Console.WriteLine(string.Format(“PI={0:#.###},e={1:#.###}”, PI, e));

                Console.WriteLine($”PI={PI:#.#####},e={e:#.#####}”);

                Console.WriteLine(“”);

            }

Output:

3.14159265358979

3.14

3.14

PI=3.142,e=2.718

PI=3.14159,e=2.71828

Due to$"PI={PI:#.#####},e={e:#.#####}"This kind of writing is the most brief one, which is adopted uniformly below.

  

1. Comma separated numbers

code:

            {

Console.WriteLine (“numbers separated by commas”);

                int a = 12345;

                double b = 1234.567;

 

                Console.WriteLine($”a={a}”);

                Console.WriteLine($”a={a:N}”);

                Console.WriteLine($”a={a:N0}”);

                Console.WriteLine($”a={a:N1}”);

                Console.WriteLine($”a={a:N2}”);

                Console.WriteLine($”a={a:N3}”);

 

                Console.WriteLine($”b={b}”);

                Console.WriteLine($”b={b:N0}”);

                Console.WriteLine($”b={b:N1}”);

                Console.WriteLine($”b={b:N2}”);              

            }

Output:

Numbers separated by commas

a=12345

a=12,345.00

a=12,345

a=12,345.0

a=12,345.00

a=12,345.000

b=1234.567

b=1,235

b=1,234.6

b=1,234.57

 

2. Zero padding and truncation

code:

            {

Console.WriteLine (“zero padding and truncation”);

                int a = 12345;

                double b = 1234.567;

 

                Console.WriteLine($”a={a}”);

                Console.WriteLine($”a={a:D3}”);

                Console.WriteLine($”a={a:D6}”);

 

                Console.WriteLine($”b={b}”);

                Console.WriteLine($”b={b:00.00}”);

                Console.WriteLine($”b={b:000000.0000}”);

                Console.WriteLine($”b={b:##.##}”);

                Console.WriteLine($”b={b:######.####}”);

                Console.WriteLine($”b={b:000000.####}”);

                Console.WriteLine($”b={b:######.0000}”);

            }

Output:

Zero padding and truncation

a=12345

a=12345

a=012345

b=1234.567

b=1234.57

b=001234.5670

b=1234.57

b=1234.567

b=001234.567

b=1234.5670

 

3. Alignment

code:

            {

Console.WriteLine (“alignment”);

                int a = 12;

                int b = 123;

                int c = 12345;

                float d = 12.34f;

 

                Console.WriteLine($”a={a,8}”);

                Console.WriteLine($”b={b,8}”);

                Console.WriteLine($”c={c,8}”);

                Console.WriteLine($”d={d,8}”);

 

                Console.WriteLine($”a=[{a,-8}]”);

                Console.WriteLine($”b=[{b,-8}]”);

                Console.WriteLine($”c=[{c,-8}]”);

                Console.WriteLine($”d=[{d,-8}]”);

            }

Output:

alignment

a=      12

b=     123

c=   12345

d=   12.34

a=[12      ]

b=[123     ]

c=[12345   ]

d=[12.34   ]

 

4. Currency

code:

           {

Console.WriteLine (the “currency”);

                int a = 123;

                float b = 123.45f;

 

                Console.WriteLine($”a={a:C}”);

                Console.WriteLine($”a={a:C1}”);

                Console.WriteLine($”a={a:C2}”);

                Console.WriteLine($”a={a:C3}”);

                Console.WriteLine($”a={a:C4}”);

 

                Console.WriteLine($”b={b:C}”);

                Console.WriteLine($”b={b:C1}”);

                Console.WriteLine($”b={b:C2}”);

                Console.WriteLine($”b={b:C3}”);

                Console.WriteLine($”b={b:C4}”);

            }

Output:

currency

a=¥123.00

a=¥123.0

a=¥123.00

a=¥123.000

a=¥123.0000

b=¥123.45

b=¥123.5

b=¥123.45

b=¥123.450

b=¥123.4500

 

5. Date

code:

            {

Console.WriteLine (the “date”);

                DateTime now = DateTime.Now;

                Console.WriteLine($”Now:{now}”);

                Console.WriteLine($”Now:{now:G}”);

                Console.WriteLine($”Now:{now:g}”);

                Console.WriteLine($”Now:{now:D}”);

                Console.WriteLine($”Now:{now:d}”);

                Console.WriteLine($”Now:{now:F}”);

                Console.WriteLine($”Now:{now:f}”);

                Console.WriteLine($”Now:{now:T}”);

                Console.WriteLine($”Now:{now:t}”);

                Console.WriteLine($”Now:{now:M}”); 

                Console.WriteLine($”Now:{now:yyyy-MM-dd HH:mm:ss}”);

            }

Output:

date

Now:2020/5/9 8:15:29

Now:2020/5/9 8:15:29

Now:2020/5/9 8:15

Now:2020 May 9, 2009

Now:2020/5/9

Now:2020 May 9, 2008 8:15:29

Now:2020 8:15, May 9, 2009

Now:8:15:29

Now:8:15

Now:5 September 9

Now:2020-05-09 08:15:29

 

6. Scientific counting

code:

            {

Console.WriteLine (the “scientific counting method”);

                double b = 123.45d;

 

                Console.WriteLine($”b={b:E}”);

                Console.WriteLine($”b={b:E0}”);

                Console.WriteLine($”b={b:E1}”);

                Console.WriteLine($”b={b:E2}”);

                Console.WriteLine($”b={b:E3}”);

                Console.WriteLine($”b={b:E10}”);

            }

Output:

Scientific counting

b=1.234500E+002

b=1E+002

b=1.2E+002

b=1.23E+002

b=1.235E+002

b=1.2345000000E+002

 

7. Percent sign

code:

    {

Console.WriteLine (“percentage sign”);

                double b = 0.456d;

 

                Console.WriteLine($”b={b:P}”);

                Console.WriteLine($”b={b:P0}”);

                Console.WriteLine($”b={b:P1}”);

                Console.WriteLine($”b={b:P2}”);

            }

Output:

Percent sign

b=45.60%

b=46%

b=45.6%

b=45.60%

 

8. Hexadecimal

code:

 

            {

Console.WriteLine (“hexadecimal”);

                int a = 0x1E2F;

                int b = 250;            

 

                Console.WriteLine($”a={a:X}”);

                Console.WriteLine($”b={b:X}”);

                Console.WriteLine($”a={a:X8}”);

                Console.WriteLine($”b={b:X8}”); 

            }

Output:

hexadecimal

a=1E2F

b=FA

a=00001E2F

b=000000FA