ASP.NET 出現訊息「伺服器應用程式無法使用」解決方法 [寫程式] 藝術?還是快速開發?
六月 19

C# string.Format輸出格式

程式.瀏覽:[ 23,305user+4,997機器人 ]給個回應

C#:
  1. 前面補0的數字字串
  2. String.Format("{0:0000}", 157); // 輸出 0157
  3.  
  4. 前後都補0的數字字串
  5. String.Format("{0:0000.0000}", 157.42); // 輸出 0157.4200
  6.  
  7. 3位數()加逗號
  8. (String.Format("{0:0,0}", 38560); // 輸出 38,560
  9.  
  10. 格式化電話號碼
  11. (String.Format("{0:(###) ###-####}", 8005551212); // 輸出 (800) 555-1212
  12.  
  13. 金額的表示
  14. (String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 0); // 這個會顯示 Zero
  15. (String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 1243.50); // 這個會顯示 $1,243.50

依照上面格式範例 ~ 套用到我的報表要呈現的欄位 ~ 如以下 ~

C#:
  1. private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  2.   {
  3.    if (e.Item.ItemIndex.ToString() != "-1")
  4.    {
  5.     e.Item.Cells[0].Text = string.Format("{0:#,0.0}",double.Parse(e.Item.Cells[3].Text.ToString()));
  6.    }
  7.   }

參考資料來源:
http://blog.stevex.net/index.php/string-formatting-in-csharp/

標籤:



文章發表日期:2009-06-19
本篇文章引用網址:

其他隨機文章:
  • FCKeditor 按鈕設定詳細說明
  • [有感] 求學路的悲歌(一)
  • 線上
  • [軟體] 超輕巧 PDF 閱讀軟體 Foxit Reader
  • (黃國華)FED金融難民營no.945


  • 1 個回應在 “C# string.Format輸出格式”

    1. lupin 說道:

      補充一個:
      前面補空格的數字字串
      String.Format("{0,6}", 157); // 輸出 " 157"

      [回應]


    寫下回應