C 中ToString格式大全

2022-03-08 08:28:39 字數 3039 閱讀 4225

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.5n 數字

2500000.tostring("n")

2,500,000.00

x 十六進製制

255.tostring("x")

ffformatcode 是可選的格式化**字串。(詳細內容請搜尋「格式化字串」檢視)

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

常用格式舉例:

(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 is 123.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 value is 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/22 22:22:22").tostring("yyyy/

mm/dd

hh:mm:ss")

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

(7) int i=123;

double j=123.45;

string s=string.format("i:,j:",i,j);

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

this.textbox1.text=s ;

//結果i:123 ,j: 123.45

**:

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 ...