我正在使用带有详细 View 的 MVC3,该 View 需要显示格式化的美元金额,例如 1,200.00 美元。在这种情况下, Controller 将 MonthlyMortgage 的 double 值传递给 View 。但是,下面的代码行没有显示正确的字符串格式。显示的是$1200.00,我需要的是$1,200.00。
我试过:
$@String.Format("{0:c}", Html.DisplayFor(model => model.MonthlyMortgage))
我试过这个:
$@String.Format("{0:#,###,###,##0.000}", Html.DisplayFor(model => model.MonthlyMortgage))
有人可以请飞机为什么这不起作用吗?
最佳答案
您不需要使用 Html.DisplayFor
,因为它会返回 MvcHtmlString
,所以 string.Format
不适用。
只需在您的模型上使用string.Format
:
@String.Format("{0:c}", Model.MonthlyMortgage)
请注意,您不再需要“$”符号,因为 {0:c}
会处理它。
关于c# - 带有货币 double 值的字符串格式显示不正确。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11175921/