16

可能重复:
使用字符串格式显示最多 2 位小数或简单整数
如何在 2 位小数中设置小数点?

我的视图中有价格字段。它有以下值2.5 and 44. 我想显示这个值2.50 and 44.00我使用下面的代码

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

在其中item.OtherFields["price"] is a object我将其转换为字符串,然后转换为十进制

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

4

4 回答 4

38

Math.Round就是这样做的 - 回合。

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

item.OtherFields["price"].ToString("0.00")
于 2012-11-08T08:41:46.057 回答
13

使用字符串格式功能

1. string.Format("{0:n2}", 200000000.8776);
2. string.Format("{0:n3}", 200000000.8776);
3. string.Format("{0:n2}", 0.3);

/* OUTOUT
1. 200,000,000.88
2. 200,000,000.878
3. 0.30
*/
于 2012-11-08T09:07:09.427 回答
4

这应该适合你

yourvalue.ToString ("0.00");
于 2012-11-08T08:36:00.800 回答
3
decimal dValue = 2.5;
string sDisplayValue = dValue.ToString("0.00");
于 2012-11-08T08:43:05.680 回答