C 學習之ToString 格式大全

2021-07-30 08:19:47 字數 3226 閱讀 8700

一、基本格式:

stringstr1=string.format("",56789);               //result: 56,789.0

stringstr2=string.format("",56789);               //result: 56,789.00

stringstr3=string.format("",56789);               //result: 56,789.000

stringstr8=string.format("",56789);               //result: 56789.0

stringstr9=string.format("",56789);               //result: 56789.00

stringstr11 =(56789 /100.0).tostring("#.##");          //result: 567.89

stringstr12 =(56789 / 100).tostring("#.##");             //result: 567

二、常用符號代表釋義:

c 或 c貨幣

console.write("", 2.5);//$2.50

console.write("", -2.5);//($2.50)

d 或 d十進位制數

console.write("", 25);//00025

e 或 e科學型

console.write("", 250000);

f 或 f固定點

console.write("", 25);

console.write("", 25); //25

g 或 g常規

console.write("", 2.5);

n 或 n數字

console.write("", 2500000);//2,500,000.00

x 或 x十六進製制

console.write("", 250); //fa

console.write("", 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是可選的格式化**字串。(詳細內容請搜尋「格式化字串」檢視)

必須用「」將格式與其他字元分開。如果恰好在格式中也要使用大括號,可以用連續的兩個大括號表示乙個大括號,即:「}」。

三、常用格式舉例:

(1) int i=12345;

this.textbox1.text=i.tostring();

//結果 12345(this指當前物件,或叫當前類的例項)

this.textbox2.text=i.tostring("d8");

//結果 00012345

(2) int i=123;

double j=123.45;

string s1=string.format("the value is",i);

string s2=string.format("the value is",j);

this.textbox1.text=s1 ;

//結果 the value is 123

this.textbox2.text=s2;

//結果 the value is123.450

(3)double i=12345.6789;

this.textbox1.text=i.tostring("f2");//結果 12345.68

this.textbox2.text=i.tostring("f6");

//結果 12345.678900

(4)double i=12345.6789;

this.textbox1.text=i.tostring("n");//結果 12,345.68

this.textbox2.text=i.tostring(「n4」); //結果12,345.6789

(5)double i=0.126;

string s=string.format("the value is",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月」);

//結果 2023年5月

convert.todatetime("2005/12/2222:22:22").tostring("yyyy/mm/dd hh:mm:ss")

"2005/12/22 22:22:22"

(7) int i=123;

double j=123.45;

strings=string.format("i:,j:",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("",1234567.891);  //預設為兩位小數,如果沒有小數字,則小數字補兩個0

或:string.format("",1234567.891)

C 中ToString格式大全

字元型轉換為字串 c 貨幣 2.5.tostring c 2.50 d 10進製數 25.tostring d5 25000 e 科學型 25000.tostring e 2.500000e 005 f 固定點 25.tostring f2 25.00 f?表示保持幾位小數 g 常規 2.5.tos...

C 中ToString格式大全

c 貨幣2.5.tostring c 2.50 d十進位制數 25.tostring d5 00025 e科學型 25000.tostring e 2.500000e 005 f固定點 25.tostring f2 25.00g常規 2.5.tostring g 2.5n 數字2500000.tos...

C 中ToString格式大全

c 貨幣2.5.tostring c 2.50 d十進位制數 25.tostring d5 e科學型 25000.tostring e 2.500000e 005 f固定點 25.tostring f2 25.00g常規 2.5.tostring g 2.5n 數字2500000.tostring ...