String.Format 格式


 

? ? ?

十進制的數字

? ?

string.Format("{0:D3}",23)

023

格式化十進制的數字

string.Format("{0:D2}",1223)

1223

精度說明符指示結果字符串中所需的最少數字個數

12345.ToString("n");

//生成 12,345.00

千位分隔,兩個小數 
用分號隔開的數字,並指定小數點后的位數

string.Format("{0:N}", 14200)

14,200.00

默認為小數點后面兩位

string.Format("{0:N3}", 14200.2458)

14,200.246

自動四舍五入

12345.ToString("e");

//生成 1.234500e+004

科學計數法

12345.ToString("f4");

//生成 12345.0000

保留小數位

12345.ToString(“f”)

?

定點

12345.ToString("x"); 
12345.ToString("x4"); 
12345.ToString("X2");

//生成 3039 (16進制)

HEX

大小寫

常規”G”和”g”

? ?

12345.ToString("G");

12345.ToString("g");

?

常規

? ? ?

自定義數字格式

? ?

String.Format("{0:00.0000}",1500.42)

1500.4200

零占位符

String.Format("{0:(#).##}",1500.42)

(1500).42

數字占位符

String.Format("{0:0.0}",1500.42)

1500.4

小數點

String.Format("{0:0,0}",1500.42)

1,500

?

String.Format("{0:0,.}",1500.42)

2

?

String.Format("{0:0%}",1500.42)

150042%

百分數

String.Format("{0:00e+0}",1500.42)

15e+2

指數占位符

? ? ?

貨幣

? ?

12345.ToString("C");

//生成 ¥12,345.00

貨幣符,千位分隔,兩個小數

string.Format("{0:C}",0.2)

¥0.20

英文操作系統結果:$0.20 
與系統的環境有關

string.Format("{0:C1}",23.15)

¥23.2

(截取會自動四舍五入)

string.Format("市場價:{0:C},優惠價{1:C}",23.15,19.82)

?

格式化多個Object實例

String.Format("{0:¥#,##0.00;(¥#,##0.00);零}", 98005.30)

¥98,005.30

金額,千位分隔符,兩個小數

String.Format("{0:¥#,##0.00;(¥#,##0.00);零}", -98005.30)

(¥98,005.30)

金額,千位分隔符,兩個小數 
負數用括號

String.Format("{0:¥#,##0.00;(¥#,##0.00);零}", 0)

金額,千位分隔符,兩個小數 
0值用文本表示

? ? ?

百分比

? ?

12345.ToString("p");

//生成 1,234,500.00%

百分數,千位分隔,兩個小數

string.Format("{0:P}", 0.24583)

24.58%

默認保留百分的兩位小數

string.Format("{0:P1}", 0.24583)

24.6%

自動四舍五入

? ? ?

往返過程

   

Double.ToString(“R”) Double.ToString(“r”)

Single.ToString(“R”) Single.ToString(“r”)

?

往返過程,適用於Double和Single

? ? ?

零占位符和數字占位符

   

string.Format("{0:0000.00}", 12394.039)

12394.04

?

string.Format("{0:0000.00}", 194.039)

0194.04

?

string.Format("{0:###.##}", 12394.039)

12394.04

?

string.Format("{0:####.#}", 194.039)

194

?
? ? ?

日期時間

? ?

string.Format("{0:d}",System.DateTime.Now)

2009-3-20

月份位置不是03

string.Format("{0:D}",System.DateTime.Now)

2009年3月20日

?

string.Format("{0:f}",System.DateTime.Now)

2009年3月20日 15:37

?

string.Format("{0:F}",System.DateTime.Now)

2009年3月20日 15:37:52

?

string.Format("{0:g}",System.DateTime.Now)

2009-3-20 15:38

?

string.Format("{0:G}",System.DateTime.Now)

2009-3-20 15:39:27

?

string.Format("{0:m}",System.DateTime.Now)

3月20日

?

string.Format("{0:t}",System.DateTime.Now)

15:41

?

string.Format("{0:T}",System.DateTime.Now)

15:41:50

?

string.Format("{0:r}",System.DateTime.Now)

Sun, 17 Jun 2012 10:12:56 GMT

RFC1123 日期格式

string.Format("{0:s}",System.DateTime.Now)

2012-06-17T10:15:12

排序日期字符串

string.Format("{0:Y}",System.DateTime.Now)

2012年6月

?

string.Format("{0:U}",System.DateTime.Now)

2012年6月17日 2:19:51

通用排序, GMT時間

string.Format("{0:u}",System.DateTime.Now)

2012-06-17 10:19:51Z

通用排序,本地時間

? ? ?

日期時間(自定義)

? ?

{0:dd}

10

Day

{0:ddd}

Tue

Day name

{0:dddd}

Tuesday

Full day name

{0:fff}

932

毫秒

{0:gg}

A.D.

Era

{0:hh}

10

2 digit hour

{0:HH}

22

2 digit hour, 24hr format

{0:mm}

38

Minute 00-59

{0:MM}

12

Month 01-12

{0:MMM}

Dec

Month abbreviation

{0:MMMM}

December

月的名字

{0:ss}

46

秒數從00到59

{0:tt}

PM

AM或PM(上午或下午)

{0:yy}

02

年(兩位數字)

{0:yyyy}

2002

{0:zz}

-05

Timezone offset, 2 digits

{0:zzz}

-05:00

Full timezone offset

{0:hh:mm:ss}

10:43:20

Separator

{0:dd/MM/yyyy}

10/12/2002

Separator

? ? ?

字符串

? ?

String.Format("->{0,10}<-", "Hello");

-> Hello<-

至少10字符,不夠前面(正數)補空

String.Format("->{0,-10}<-", "Hello")

->Hello <-

至少10字符,不夠后面(負數)補空

? ? ?

電話號碼自定義

? ?

String.Format("{0:(###) ###-####}", 18005551212)

(1800) 555-1212

?

String.Format("{0:(##)####-########}", 86075487870252)

(86)0754-87870252

?

String.Format("{0:(##)####-########}", 086075487870252)

(86)0754-87870252

?
? ? ?

零占位符”0”

? ?

int i=120; 
Console.WriteLine(i.ToString("00,000.00")); 
Console.WriteLine("{0:00,000.00}",i);

00,120.00

?
? ? ?

數字占位符”#”

? ?

double d=12050.8520; 
Console.WriteLine(d.ToString("##,###.##")); 
Console.WriteLine("{0:##,###.##}",d);

12,050.85

?
? ? ?
? ? ?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM