C 中ToString格式大全

2021-06-18 21:21:44 字數 3304 閱讀 3424

用dataformatstring格式化gridview

在gridview裡面顯示資料,要顯示的資料有好多位小數,就想讓它只顯示兩位小數,在delphi裡,直接用displayformat就行了,在.net中,查了半天msdn,發現使用dataformatstring是可以實現這個功能的,但是怎麼設定就不起作用,最後發現,由於2.0出於安全性的考慮,還要同時設定htmlencode = false,才能夠使dataformatstring生效.

留個記號,下次用的時候,就不用浪費n多時間了.

還有還有,dataformatstring = "",是預設格式,顯示兩位小數,如果需要顯示的小數字數為其他值,dataformatstring = ""即可.

dataformatstring=""

在dataformatstring 中的 表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;

數字、貨幣格式:

在指定的格式符號後可以指定小數所要顯示的位數。例如原來的資料為「1.56」,若格式設定為 ,則輸出為「1.5」。其常用的數值格式如下表所示:

格式字串 輸入 結果

"" 12345.6789 $12,345.68

"" -12345.6789 ($12,345.68)

"" 12345 12345

"" 12345 00012345

"" 12345.6789 1234568e+004

"" 12345.6789 1.2345678900e+004

"" 12345.6789 12345.68

"" 12345.6789 12346

"" 12345.6789 12345.6789

"" 123456789 1.234568e8

"" 12345.6789 12,345.68

"" 123456789 123,456,789.0000

"total: " 12345.6789 total: $12345.68

常用的日期時間格式:

格式 說明 輸出格式

d 精簡日期格式 mm/dd/yyyy

d 詳細日期格式 dddd, mmmm dd, yyyy

f 完整格式 (long date + short time) dddd, mmmm dd, yyyy hh:mm

f完整日期時間格式

(long date + long time)

dddd, mmmm dd, yyyy hh:mm:ss

g 一般格式 (short date + short time) mm/dd/yyyy hh:mm

g 一般格式 (short date + long time) mm/dd/yyyy hh:mm:ss

m,m 月日格式 mmmm dd

s 適中日期時間格式 yyyy-mm-dd hh:mm:ss

t 精簡時間格式 hh:mm

t 詳細時間格式 hh:mm:ssc貨幣

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