.NET字符串格式化

 概述:

我们在将一个类型(通常是数字和时间)转换成字符串时,可以控制其显示的样式。

基本的写法就是在调用ToString或Format方法时提供格式化参数,由于具体的参数记不住,每次用到都要上网搜索一番,浪费时间,所以总结一下以备忘。

代码:

           {

                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("");

            }

输出:

3.14159265358979

3.14

3.14

PI=3.142,e=2.718

PI=3.14159,e=2.71828

 由于$"PI={PI:#.#####},e={e:#.#####}"这种写法是最简介的,下面统一采用这种写法。

  

1、逗号隔开的数字

代码:

            {

                Console.WriteLine("用逗号隔开的数字");

                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}");              

            }

输出:

用逗号隔开的数字

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、补零和截断

代码:

            {

                Console.WriteLine("补零和截断");

                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}");

            }

输出:

补零和截断

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、对齐

代码:

            {

                Console.WriteLine("对齐");

                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}]");

            }

输出:

对齐

a=      12

b=     123

c=   12345

d=   12.34

a=[12      ]

b=[123     ]

c=[12345   ]

d=[12.34   ]

 

4、货币

代码:

           {

                Console.WriteLine("货币");

                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}");

            }

 输出:

货币

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、日期

代码:

            {

                Console.WriteLine("日期");

                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}");

            }

输出:

日期

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

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

Now:2020/5/9 8:15

Now:2020年5月9日

Now:2020/5/9

Now:2020年5月9日 8:15:29

Now:2020年5月9日 8:15

Now:8:15:29

Now:8:15

Now:5月9日

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

 

6、科学计数法

代码:

            {

                Console.WriteLine("科学计数法");

                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}");

            }

输出:

科学计数法

b=1.234500E+002

b=1E+002

b=1.2E+002

b=1.23E+002

b=1.235E+002

b=1.2345000000E+002

 

7、百分号

代码:

    {

                Console.WriteLine("百分号");

                double b = 0.456d;

 

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

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

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

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

            }

输出:

百分号

b=45.60%

b=46%

b=45.6%

b=45.60%

 

8、十六进制

代码:

 

            {

                Console.WriteLine("十六进制");

                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}"); 

            }

输出:

十六进制

a=1E2F

b=FA

a=00001E2F

b=000000FA

posted @ 2020-05-13 15:24  seabluescn  阅读(967)  评论(2编辑  收藏  举报