当前位置: 移动技术网 > IT编程>开发语言>c# > C#中ToString数据类型格式大全(千分符)

C#中ToString数据类型格式大全(千分符)

2019年07月18日  | 移动技术网IT编程  | 我要评论

用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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网