如何在mvc3,C#中将数字显示到小数点后2位? [英] How to display number to 2 decimal places in mvc3,C#?

查看:354
本文介绍了如何在mvc3,C#中将数字显示到小数点后2位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆