c# - 如何在mvc3,C#中将数字显示到小数点后两位?

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

<分区>

Possible Duplicate:
Using String Format to show decimal upto 2 places or simple integer
How to set decimal point in 2 decimal places?

我的 View 中有价格字段。它具有以下值 2.5 和 44。 我想将此值显示为 2.50 和 44.00 我使用以下代码

@{decimal prolistprice = decimal.Parse(item.OtherFields["Price"].ToString());}
  $@Math.Round(prolistprice, 2, MidpointRounding.AwayFromZero)

其中 item.OtherFields["price"] 是一个对象 我将它转换为字符串,然后是小数

但是 Math.round 不工作它只显示 2.5 和 44.. 任何人都可以帮助这个

最佳答案

Math.Round 就是这样做的 - 圆。

要格式化您可以使用 .ToString(formatString) 的数字,如下所示:

item.OtherFields["price"].ToString("0.00")

关于c# - 如何在mvc3,C#中将数字显示到小数点后两位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13285240/

相关文章:

asp.net - 使用资源文件本地化

javascript - 如何用 Jquery/JS 交换 2 @Html.DropDownListFor

c# - Web api 上的 Azure 预热不起作用

c# - 在 ASP.NET 编码引号中,阻止请求到达路由

javascript - html 元素在单选按钮列表中不起作用

asp.net-mvc-3 - ASP.NET MVC 3 - 模型验证

c# - 试图从Polar H10获得心率变异性[蓝牙低能耗样本UWP]

c# - 非泛型类型的协变/逆变支持吗?

c# - RavenDB 读取性能

c# - 使用 C# 从字符串中提取数字