在MVC 3 Grid中格式化双/十进制值

时间:2012-03-19 15:02:33

标签: c# asp.net-mvc asp.net-mvc-3 format

我真的无法弄明白 如何格式化双/小数值

12,123,123.78

这是我的HTML

@grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
                            grid.Column(format: (item) => Html.ActionLink("Candy Bar", "Products", new { id = item.ChainID })),
                                       grid.Column(format: (item) => Html.ActionLink("Per Capita", "ProductsByCategory", new { id = item.ChainID })),
                                 grid.Column("CinemaName", "Complejo"),
                                              grid.Column("TotalSum2", "Monto").Format(  ???? )
)

如果我这样做

grid.Column("TotalSum2", "Monto", format: (item) => item.TotalSum2.ToString("0:C"))

我明白了:

270223:C

和这个

"{0:n0}"

给出

{27022:N3}


解决方案:

grid.Column("TotalSum2", "Monto", format: (item) => item.TotalSum2.ToString("#,0.00"))

2 个答案:

答案 0 :(得分:1)

在当前主题上设置正确的CultureInfo或使用DisplayFormatAttribute

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayformatattribute.aspx

答案 1 :(得分:1)

看看CultureInfo and format strings

grid.Column("TotalSum2", "Monto", format: (item) => item.TotalSum2.ToString("#,#"))