C#數字格式化輸出

iDotNetSpace發表於2010-04-20

int a = 12345678;
//格式為sring輸出
Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);
Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";
Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",a);//asdfadsf¥1,234.00adsfasdf
Label2.Text = "asdfadsf"+a.ToString("C")+"adsfasdf";//asdfadsf¥1,234.00adsfasdf
double b = 1234.12543;
int a = 12345678;

//格式為特殊的string樣式輸出
Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",b);//asdfadsf¥1,234.13adsfasdf
Label2.Text = "asdfadsf"+b.ToString("C")+"adsfasdf";//asdfadsf¥1,234.13adsfasdf
Label1.Text = string.Format("{0:C3}",b);//¥1,234.125
Label2.Text = b.ToString("C3");//¥1,234.125
Label1.Text = string.Format("{0:d}",a);//十進位制--12345678
Label2.Text = b.ToString("d");//十進位制--相同的型別,轉換報錯
Label1.Text = string.Format("{0:e}",a);//指數--1.234568e+007
Label2.Text = b.ToString("e");//指數--1.234125e+003
Label1.Text = string.Format("{0:f}",a);//定點數--12345678.00
Label2.Text = b.ToString("f");//定點數--1234.13
Label1.Text = string.Format("{0:n}",a);//數值--12,345,678.00
Label2.Text = b.ToString("n");//數值--1,234.13
Label1.Text = string.Format("{0:x}",a);//十六進位制--bc614e
Label2.Text = b.ToString("x");//16--帶有小數不能轉換,出錯
Label1.Text = string.Format("{0:g}",a);//通用為最緊湊--12345678
Label2.Text = b.ToString("g");//通用為最緊湊--1234.12543
Label1.Text = string.Format("{0:r}",a);//轉來轉去不損失精度--整數不允許用,報錯
Label2.Text = b.ToString("r");//轉來轉去不損失精度--1234.12543

double b = 4321.12543;
int a = 1234;
自定義模式輸出:

//"0"描述:佔位符,如果可能,填充位
Label1.Text = string.Format("{0:000000}",a);// 001234
Label2.Text = string.Format("{0:000000}",b);// 004321

//"#"描述:佔位符,如果可能,填充位
Label1.Text = string.Format("{0:#######}",a);// 1234
Label2.Text = string.Format("{0:#######}",b);// 4321
Label1.Text = string.Format("{0:#0####}",a);// 01234
Label2.Text = string.Format("{0:0#0000}",b);// 004321

//"."描述:小數點
Label1.Text = string.Format("{0:000.000}",a);//1234.000
Label2.Text = string.Format("{0:000.000}",b);//4321.125
double b = 87654321.12543;
int a = 12345678;

//","描述:數字分組,也用於增倍器
Label1.Text = string.Format("{0:0,00}",a);// 12,345,678
Label2.Text = string.Format("{0:0,00}",b);// 87,654,32
Label1.Text = string.Format("{0:0,}",a);// 12346
Label2.Text = string.Format("{0:0,}",b);// 87654
Label1.Text = string.Format("{0:0,,}",a);// 12
Label2.Text = string.Format("{0:0,,}",b);// 88
Label1.Text = string.Format("{0:0,,,}",a);// 0
Label2.Text = string.Format("{0:0,,,}",b);// 0

//"%"描述:格式為百分數
Label1.Text = string.Format("{0:0%}",a);// 1234567800%
Label2.Text = string.Format("{0:#%}",b);// 8765432113%
Label1.Text = string.Format("{0:0.00%}",a);// 1234567800.00%
Label2.Text = string.Format("{0:#.00%}",b);// 8765432112.54%

//"abc"描述:顯示單引號內的文字
Label1.Text = string.Format("{0:'文字'0}",a);// 文字12345678
Label2.Text = string.Format("{0:文字0}",b);// 文字87654321

//"\"描述:後跟1要列印字的字元,也用於轉移符\n等
Label1.Text = string.Format("\"你好!\"");// "你好!"
Label2.Text = string.Format("[url=file://\\c\\books\\new\\we.asp]\\c\\books\\new\\we.asp");//\c\books\new\we.asp

//"@"描述:後跟要列印字的字元,
Label1.Text = string.Format(@"""你好!"""); // "你好!"要列印"則需要輸入兩對才可以
Label2.Text = string.Format(@"\c\books\new\we.asp");//\c\books\new\we.asp

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-660264/,如需轉載,請註明出處,否則將追究法律責任。

相關文章