ToString 格式和用法大全

2021-08-20 00:22:40 字數 3306 閱讀 2544

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.tostring("n")

2,500,000.00

x十六進製制

255.tostring("x")

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

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

常用格式舉例:

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

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

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

//結果 00012345

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

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

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

//結果 12345.678900

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

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

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%

this.textbox1.text=dt.tostring("yy.m.d");

//結果 03.5.25

this.textbox2.text=dt.tostring(「yyyy年m月」);

//結果 2023年5月

double j=123.45;

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

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

this.textbox1.text=s ;

//結果i:123 ,j: 123.45 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.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月

(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 

ToString 格式和用法大全 網摘 收藏

tostring 方法是最經常使用,也需要使用的方法,可是不少人都只用到簡單的無引數的方法,而實際上tostring 有4個過載方法,你轉換成不同樣式的方法,他們都乙個乙個引數那就是格式引數,以顯示成你需要的格式。如下,可以轉換成7種格式的 c貨幣 2.5.tostring c 2.50 d十進位制...

ToString格式大全二

1.y代表年份,注意是小寫的y,大寫的 並不代表年份。2.m表示月份。3.d表示日期,注意d並不代表什麼。4.h或h表示小時,h用的是12小時制,h用的是24小時制。5.m表示分鐘。6.s表示秒。注意s並不代表什麼。格式 輸出 示例 年y string yy datetime.now.tostrin...

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