C# 格式化輸出(格式化雙精度數值小數位、字符串等)


  • 格式化雙精度數值小數位

Double dValue = 95.12345;
int iValue = 10000;
string strValue = "95.12345";
string result = "";

result = Convert.ToDouble(dValue).ToString("0.00");//保留小數點后兩位,結果為95.12
result = Convert.ToDouble(iValue).ToString("0.00");//10000.00 
result = Convert.ToDouble(strValue).ToString("0.00");//95.12

result = Convert.ToDouble(dValue).ToString("P");//得到小數點后2位的百分比,自動 加上%號;//9512.35%
result = Convert.ToDouble(strValue).ToString("f4");//保留小數點后4位;  //95.1235
//要注意的一點是 Convert.ToDouble一定要是這種雙精度的,不然會報錯。
  • String具體的格式化數據的方法

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;
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("f4");                                     //定點數--1234.1254
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
Label2.Text = b.ToString("g2");//通用為最緊湊1.2e+03
Label1.Text = string.Format("{0:r}",a);//轉來轉去不損失精度--整數不允許用,報錯
Label2.Text = b.ToString("r");//轉來轉去不損失精度--1234.12543
b = 4321.12543;
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
b = 87654321.12543;
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("//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

百分數格式應該用“p”這個參數。
格式 原始數據 結 果
"{0:P}" 0.40 40%


數字 {0:N2} 12.36
數字 {0:N0} 13
貨幣 {0:c2} $12.36
貨幣 {0:c4} $12.3656
貨幣 "¥{0:N2}" ¥12.36
科學計數法 {0:E3} 1.23E+001
百分數 {0:P} 12.25% P and p present the same.
日期 {0:D} 2006年11月25日
日期 {0:d} 2006-11-25
日期 {0:f} 2006年11月25日 10:30
日期 {0:F} 2006年11月25日 10:30:00
日期 {0:s} 2006-11-26 10:30:00
時間 {0:T} 10:30:00
DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
Label11.Text = dt.Add(?).ToString();//問號為一個時間段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime
Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
Label1.Text = string.Format("{0:d}",dt);//2005-11-5
Label2.Text = string.Format("{0:D}",dt);//2005年11月5日
Label3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text = string.Format("{0:M}",dt);//11月5日
Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text =string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);

 

----

DateTime dt =DateTime.Now;
string.Format("{0:y yy yyy yyyy}",dt); //17 17 2017 2017
string.Format("{0:M MM MMM MMMM}", dt);//4  04 四月 四月
string.Format("{0:d dd ddd dddd}", dt);//1  01 周六 星期六
string.Format("{0:t tt}", dt);//下 下午
string.Format("{0:H HH}", dt);//13 13
string.Format("{0:h hh}", dt);//1  01
string.Format("{0:m mm}", dt);//16 16
string.Format("{0:s ss}", dt);//32 32
string.Format("{0:F FF FFF FFFF FFFFF FFFFFF FFFFFFF}", dt);//1 1  108 108  108   108    108
string.Format("{0:f ff fff ffff fffff ffffff fffffff}", dt);//1 10 108 1080 10800 108000 1080000
string.Format("{0:z zz zzz}", dt);//+8 +08 +08:00

string.Format("{0:yyyy/MM/dd HH:mm:ss.fff}",dt);  //2017/04/01 13:16:32.108
string.Format("{0:yyyy/MM/dd dddd}", dt);      //2017/04/01 星期六
string.Format("{0:yyyy/MM/dd dddd tt hh:mm}", dt); //2017/04/01 星期六 下午 01:16
string.Format("{0:yyyyMMdd}", dt);         //20170401
string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", dt); //2017-04-01 13:16:32.108

---

string str1 =string.Format("{0:N1}",56789); //result: 56,789.0
string str2 =string.Format("{0:N2}",56789); //result: 56,789.00
string str3 =string.Format("{0:N3}",56789); //result: 56,789.000
string str8 =string.Format("{0:F1}",56789); //result: 56789.0
string str9 =string.Format("{0:F2}",56789); //result: 56789.00
string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89
string str12 =(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

c# String 前面不足位數補零的方法

1.string intmp = string.Format("{0:D6}", intNum);
2.string intmp = intNum.ToString("D6");
3.string intmp = intNum.ToString().PadLeft(6, '0'); 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM