C# .ToString()格式大全


 C#.ToString()格式大全

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

C 貨幣

 2.5.ToString("C")  ¥2.50

D  十進制數

25.ToString("D5")    00025

E科學型

 25000.ToString("E")   2.500000E+005

F  固定點

 25.ToString("F2")    25.00

G 常規

 2.5.ToString("G")     2.5

N 數字

 2500000.ToString("N")      2,500,000.00

X  十六進制

 255.ToString("X")     FF

formatCode是可選的格式化代碼字符串。(詳細內容請搜索“格式化字符串”查看)

必須用“{”和“}”將格式與其他字符分開。如果恰好在格式中也要使用大括號,可以用連續的兩個大括號表示一個大括號,即:“{{”或者“}}”。

常用格式舉例:

(1int i=12345;

this.textBox1.Text=i.ToString();

//結果 12345(this指當前對象,或叫當前類的實例)

this.textBox2.Text=i.ToString("d8");

//結果 00012345
2int i=123;

double j=123.45;

string s1=string.Format("the value is{0,7:d}",i);

string s2=string.Format("the value is{0,7:f3}",j);

this.textBox1.Text=s1 ;

//結果 the value is 123

this.textBox2.Text=s2;

//結果 the value is123.450
3double i=12345.6789;

this.textBox1.Text=i.ToString("f2");//結果 12345.68

this.textBox2.Text=i.ToString("f6");

//結果 12345.678900
4double i=12345.6789;

this.textBox1.Text=i.ToString("n");//結果 12,345.68

this.textBox2.Text=i.ToString(“n4”); //結果12,345.6789
5double i=0.126;

string s=string.Format("the value is{0:p}",i);

this.textBox1.Text=i.ToString("p");//結果 12.6%

this.textBox2.Text=s; //結果 the valueis 12.6%
6) DateTime dt =new DateTime(2003,5,25);

this.textBox1.Text=dt.ToString("yy.M.d");

//結果 03.5.25

this.textBox2.Text=dt.ToString(“yyyy年M月”);

//結果 2003年5月

Convert.ToDateTime("2005/12/2222:22:22").ToString("yyyy/MM/dd HH:mm:ss")

"2005/12/22 22:22:22"7int i=123;

double j=123.45;

strings=string.Format("i:{0,-7},j:{1,7}",i,j);

//-7表示左對齊,占7位

this.textBox1.Text=s ;

//結果i:123 ,j: 123.45

 

 

補充:

1、C#中用最簡單的方法把數字(不含小數)轉換為千分位格式:
     如1234567變成1,234,567

    方法:1234567.ToString("###,###")  或  1234567.ToString("N0")  

 

2、C#中把數字轉換成帶兩位小數的千分位字符:

    如1234567.891變成1,234,567.89

    方法:String.Format("{0:N}",1234567.891);  //默認為兩位小數,如果沒有小數位,則小數位補兩個0

    或:String.Format("{0:N2}",1234567.891);

 


免責聲明!

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



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