如何在mvc3,C#中将数字显示到小数点后2位? [英] How to display number to 2 decimal places in mvc3,C#?
问题描述
Possible Duplicate:
Using String Format to show decimal upto 2 places or simple integer
How to set decimal point in 2 decimal places?
我的视图中有价格字段.它具有以下值2.5 and 44.
我想将此值显示为2.50 and 44.00
我使用以下代码
I have Price field in my view. it have the following values 2.5 and 44.
I want to display this value to 2.50 and 44.00
i use the following code
@{decimal prolistprice = decimal.Parse(item.OtherFields["Price"].ToString());}
$@Math.Round(prolistprice, 2, MidpointRounding.AwayFromZero)
其中item.OtherFields["price"] is a object
我将其转换为字符串,然后转换为十进制
in which item.OtherFields["price"] is a object
i convert it to string and then decimal
但是Math.round无法正常工作,它仅显示2.5和44. 谁能帮忙
but Math.round is not working it shows 2.5 and 44 only.. Can anyone help this
推荐答案
Math.Round
就是这样做的-圆.
要设置数字格式,您可以像下面这样使用.ToString(formatString)
:
To format the number you may use .ToString(formatString)
like so:
item.OtherFields["price"].ToString("0.00")
这篇关于如何在mvc3,C#中将数字显示到小数点后2位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!