用DataFormatString花样化GridView
在 GridView内里显现数据,要显现的数据有好多位小数,就想让它只显现两位小数,在delphi里,直接用DisplayFormat就好了, 在.net中,查了半天msdn,发明运用DataFormatString是能够完成这个功用的,然则怎样设置就不起作用,末了发明,因为2.0出于安 全性的斟酌,还要同时设置HtmlEncode = false,才能够使DataFormatString见效.
留个暗号,下次用的时刻,就不用浪费N多时候了.
另有另有,DataFormatString = "{0:F}",是默许花样,显现两位小数,假如须要显现的小数位数为其他值,DataFormatString = "{0:Fn}"即可.
DataFormatString="{0:花样字符串}"
在DataFormatString 中的 {0} 示意数据自身,而在冒号背面的花样字符串代表所们愿望数据显现的花样;
数字、钱银花样:
在指定的花样标记后能够指定小数所要显现的位数。比方本来的数据为「1.56」,若花样设定为 {0:N1},则输出为「1.5」。其经常使用的数值花样如下表所示:
花样字符串 输入 效果
"{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
经常使用的日期时候花样:
花样 申明 输出花样
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:ss
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 {0,7:d}",i); string s2=string.Format("the value is {0,7:f3}",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 {0:p}",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月”); //效果 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示意左对齐,占7位 this.textBox1.Text=s ; //效果i:123 ,j: 123.45
以上就是C#中ToString数据类型花样大全(千分符)总结的细致内容,更多请关注ki4网别的相干文章!