C#手机格式化后如何恢复数据数据的几种写法

开源中国在线工具
在线代码格式化
待格式化XML:
格式化XML:
在此填写您的意见信息:
Email邮箱: (必填)
您的建议和意见:用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}"
$12,345.68 "{0:C}" - ($12,345.68) "{0:D}"
"{0:D8}" 45 "{0:E}"
4 "{0:E10}"
1.E+004 "{0:F}"
12345.68 "{0:F0}"
12346 "{0:G}"
"{0:G7}" . "{0:N}"
12,345.68 "{0:N4}" 3,456,789.0000 "Total: {0:C}"
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
2.5.ToString("C")
25.ToString("D5")
25000.ToString("E")
25.ToString("F2")
2.5.ToString("G")
2500000.ToString("N")
2,500,000.00
255.ToString("X")
formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)
必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。
常用格式举例:
(1) int i=12345;
this.textBox1.Text=i.ToString();
//结果 12345(this指当前对象,或叫当前类的实例)
this.textBox2.Text=i.ToString("d8");
(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=;
this.textBox1.Text=i.ToString("f2"); //结果 12345.68
this.textBox2.Text=i.ToString("f6");
(4)double i=;
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();
this.textBox1.Text=dt.ToString("yy.M.d");
//结果 03.5.25
this.textBox2.Text=dt.ToString(“yyyy年M月”);
//结果 2003年5月
Convert.ToDateTime(" 22:22:22").ToString("yyyy/MM/dd HH:mm:ss") " 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=
//结果i:123 ,j: 123.45格式规范的完整形式:{index [,width][:formatstring]}
index是此格式程序引用的格式字符串之后的参数,从零开始计数;width(可选) 是要设置格式的字段的宽度,width取正数表示结果右对齐,取负数表示结果左对齐;formatstring(可选),包含有关设置类型格式的格式说明。
一.内置类型的字母格式
1. E-科学计数法表示
& & 25000.ToString("E");//结果:2.4
2. C-货币表示,带有逗号分隔符,默认小数点后保留两位,四舍五入
& & 2.5.ToString("C");//结果:¥&2.50
3. D-十进制数
& & 25.ToString("D5");//结果:00025
4. F-浮点数,保留小数位数(四舍五入)
& & 25.ToString("F2");//结果:25.00
5. G-常规,保留指定位数的有效数字,四舍五入
& & 2.52.ToString("G2");//结果:2.5&
6. N-带有逗号分隔符,默认小数点后保留两位,四舍五入&& & 2500000.ToString("N");//结果:2,500,000.00
7. X-十六进制,非整型将产生格式异常
& & 255.ToString("X");//结果:FF&
二. 自定义模式输出
1. &0&描述:占位符,如果可能,填充位
& & string.Format("{0:000000}",1234);//结果:001234
2. &#&描述:占位符,如果可能,填充位
& & string.Format("{0:######}",1234);//结果:1234
& & string.Format("{0:#0####}",1234);//结果:01234
& & string.Format("{0:0#0####}",1234);//结果:0001234
3. "."描述:小数点
& & string.Format("{0:000.000}",1234);//结果:
& & string.Format("{0:000.000}",);//结果:
4. ","描述:数字分组,也用于增倍器
& & string.Format("{0:0,0}",1234567);//结果:1,234,567
5. "%"描述:格式为百分数
& & string.Format("{0:0%}",1234);//结果:123400%
& & string.Format("{0:#%}", );//结果:123413%
& & string.Format("{0:0.00%}",1234);//结果: %
& & string.Format("{0:#.00%}",);//结果:%
6. "abc"描述:显示单引号内的文本
& & string.Format("{0:'文本'0}",1234);//结果:文本1234
7. "\"描述:用于转移符
& & string.Format("\"abc!\"");//结果:abc!
8. "@"描述:后跟要打印字符串
& & string.Format(@""abc!"");//结果:abc!
阅读(...) 评论()1銆佷綔涓哄弬鏁}

我要回帖

更多关于 asp添加数据的参数化写法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信