C#數字格式化輸出)

來源:互聯網
上載者:User
文章目錄
  • C#數字格式化輸出
C#數字格式化輸出

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

轉至:http://daiwen.blog.ccidnet.com

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.