您的位置:首页 > 其它

DataFormatString--格式化字符串

2012-01-07 10:14 399 查看
这里需要注意以下几点

1:由于2.0出于安全性的考虑,还要同时设置HtmlEncode = false,才能够使DataFormatString生效。在GridView中的asp:BoundField使用DataFormatString必须设置属性 HtmlEncode="False",否则不起作用。

2:如果需要使用日期类型的格式化字符串,必须数据实体中对应的字段也应该日起类型的。

3:格式化字符串C代表货币单位,需要绑定的数据类型应该是数字类型的。如果是字符串类型的不起作用,需要手动添加格式化字符串为DataFormatString="¥{0:C}"。

使用 DataFormatString 属性来提供列中各项的自定义格式。

获取或设置指定列中各项的显示格式的字符串。指定列中各项的显示格式的格式化字符串,默认值为 String.Empty。

  数据格式字符串由以冒号分隔的两部分组成,形式为 { A : Bxx }。例如,格式化字符串 {0:F2} 将显示带两位小数的定点数。  

  整个字符串必须放在大括号内,表示它是格式字符串,而不是实际字符串。大括号外的任何文本均显示为实际文本。

  冒号前的值指定在从零开始的参数列表中的参数索引。值只能设置为 0,因为每个单元格中只有一个值。

  冒号后的字符指定值的显示格式。下表列出了一些常用格式。

程序:

public partial class FormatStrings : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

double x = 2005.5;

int y = 128;

double z = 0.25;

Response.Write(x.ToString() + " 以C2格式化之后 " + String.Format("{0:C2}", x) + "<BR>");

Response.Write(y.ToString() + " 以D格式化之后 " + String.Format("{0:D}", y) + "<BR>");

Response.Write(x.ToString() + " 以E2格式化之后 " + String.Format("{0:E2}", x) + "<BR>");

Response.Write(x.ToString() + " 以F4格式化之后 " + String.Format("{0:F4}", x) + "<BR>");

Response.Write(x.ToString() + " 以G格式化之后 " + String.Format("{0:G}", x) + "<BR>");

Response.Write(x.ToString() + " 以N3格式化之后 " + String.Format("{0:N3}", x) + "<BR>");

Response.Write(z.ToString() + " 以P格式化之后 " + String.Format("{0:P}", z) + "<BR>");

Response.Write(y.ToString() + " 以X格式化之后 " + String.Format("{0:X}", y) + "<BR>");

Response.Write(x.ToString() + " 以00####.00格式化之后 " + String.Format("{0:00####.00}", x) + "<BR>");

}

}

效果:

2005.5 以C2格式化之后 ¥2,005.50

128 以D格式化之后 128

2005.5 以E2格式化之后 2.01E+003

2005.5 以F4格式化之后 2005.5000

2005.5 以G格式化之后 2005.5

2005.5 以N3格式化之后 2,005.500

0.25 以P格式化之后 25.00%

128 以X格式化之后 80

2005.5 以00####.00格式化之后 002005.50

更多:

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

<%# String.Format("{0:yyyy-MM-dd} ", Eval("EffectiveDate "))%>

<%# String.Format("{0:yyyy-M-d} ", DataBinder.Eval(Container.DataItem, "EffectiveDate "))%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: