欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中ToString數(shù)據(jù)類型格式大全(千分符)

 更新時(shí)間:2017年02月16日 21:38:13   投稿:mdxy-dxy  
這篇文章主要介紹了C#中ToString數(shù)據(jù)類型格式大全 千分符,需要的朋友可以參考下

用DataFormatString格式化GridView
在 GridView里面顯示數(shù)據(jù),要顯示的數(shù)據(jù)有好多位小數(shù),就想讓它只顯示兩位小數(shù),在delphi里,直接用DisplayFormat就行了, 在.net中,查了半天msdn,發(fā)現(xiàn)使用DataFormatString是可以實(shí)現(xiàn)這個(gè)功能的,但是怎么設(shè)置就不起作用,最后發(fā)現(xiàn),由于2.0出于安 全性的考慮,還要同時(shí)設(shè)置HtmlEncode = false,才能夠使DataFormatString生效.
留個(gè)記號(hào),下次用的時(shí)候,就不用浪費(fèi)N多時(shí)間了.
還有還有,DataFormatString = "{0:F}",是默認(rèn)格式,顯示兩位小數(shù),如果需要顯示的小數(shù)位數(shù)為其他值,DataFormatString = "{0:Fn}"即可.

DataFormatString="{0:格式字符串}"

在DataFormatString 中的 {0} 表示數(shù)據(jù)本身,而在冒號(hào)后面的格式字符串代表所們希望數(shù)據(jù)顯示的格式;

數(shù)字、貨幣格式:
在指定的格式符號(hào)后可以指定小數(shù)所要顯示的位數(shù)。例如原來的數(shù)據(jù)為「1.56」,若格式設(shè)定為 {0:N1},則輸出為「1.5」。其常用的數(shù)值格式如下表所示:

格式字符串 輸入 結(jié)果
"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68

常用的日期時(shí)間格式:

格式 說明 輸出格式
d 精簡(jiǎn)日期格式 MM/dd/yyyy
D 詳細(xì)日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時(shí)間格式
(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 適中日期時(shí)間格式 yyyy-MM-dd HH:mm:ss
t 精簡(jiǎn)時(shí)間格式 HH:mm
T 詳細(xì)時(shí)間格式 HH:mm:ss

C

貨幣

2.5.ToString("C")

¥2.50

D

十進(jìn)制數(shù)

25.ToString("D5")

00025

E

科學(xué)型

25000.ToString("E")

2.500000E+005

F

固定點(diǎn)

25.ToString("F2")

25.00

G

常規(guī)

2.5.ToString("G")

2.5

N

數(shù)字

2500000.ToString("N")

2,500,000.00

X

十六進(jìn)制

255.ToString("X")

FF

formatCode 是可選的格式化代碼字符串。(詳細(xì)內(nèi)容請(qǐng)搜索“格式化字符串”查看)
必須用“{”和“}”將格式與其他字符分開。如果恰好在格式中也要使用大括號(hào),可以用連續(xù)的兩個(gè)大括號(hào)表示一個(gè)大括號(hào),即: “{{”或者“}}”。

常用格式舉例:

(1) int i=12345;

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

//結(jié)果 12345(this指當(dāng)前對(duì)象,或叫當(dāng)前類的實(shí)例)

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

//結(jié)果 00012345

(2) int 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 ;

//結(jié)果 the value is 123

this.textBox2.Text=s2;

//結(jié)果 the value is 123.450

(3)double i=12345.6789;

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

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

//結(jié)果 12345.678900

(4)double i=12345.6789;

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

this.textBox2.Text=i.ToString(“n4”); //結(jié)果 12,345.6789

(5)double i=0.126;

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

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

this.textBox2.Text=s; //結(jié)果 the value is 12.6%

(6) DateTime dt =new DateTime(2003,5,25);

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

//結(jié)果 03.5.25

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

//結(jié)果 2003年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:{0,-7},j:{1,7}",i,j);

//-7表示左對(duì)齊,占7位

this.textBox1.Text=s ;

//結(jié)果i:123 ,j: 123.45

相關(guān)文章

最新評(píng)論