[轉載]C#的String.Format格式化舉例

來源:互聯網
上載者:User

stringstr1 =string.Format("{0:N1}",56789);               //result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789);               //result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789);               //result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789);               //result: 56789.0
stringstr9 =string.Format("{0:F2}",56789);               //result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##");           //result: 567.89
stringstr12 =(56789 / 100).ToString("#.##");             //result: 567

C 或 c
貨幣
Console.Write("{0:C}", 2.5);  //$2.50
Console.Write("{0:C}", -2.5); //($2.50)

D 或 d
十進位數
Console.Write("{0:D5}", 25);  //00025
 
E 或 e
科學型
Console.Write("{0:E}", 250000);  //2.500000E+005
 
F 或 f
固定點
Console.Write("{0:F2}", 25);  //25.00
Console.Write("{0:F0}", 25);  //25
 
G 或 g
常規
Console.Write("{0:G}", 2.5);  //2.5
 
N 或 n
數字
Console.Write("{0:N}", 2500000);  //2,500,000.00
 
X 或 x
十六進位
Console.Write("{0:X}", 250);  //FA
Console.Write("{0:X}", 0xffff);  //FFFF

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.