字符串格式化

内容可以参考:http://www.cnblogs.com/jobs2/p/3948049.html

  • 用分号隔开的数字,并指定小数点后的位数

string.Format("{0:N}", 14200) 结果为:14,200.00  (默认为小数点后面两位)

string.Format("{0:N3}", 14200.2458) 结果为:14,200.246 (自动四舍五入)

  • 格式化百分比

string.Format("{0:P}", 0.24583) 结果为:24.58% (默认保留百分的两位小数)

string.Format("{0:P1}", 0.24583) 结果为:24.6% (自动四舍五入)

  • 日期格式化

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



原文地址:https://www.cnblogs.com/jacketlin/p/5687480.html