How to display a number up to two decimal places in mvc3, c #?

Possible duplicate:
Using String Format to display decimal to 2 places or a simple integer
How to set decimal point in 2 decimal places?

I have a Price field in my opinion. it has the following values 2.5 and 44. I want to show this value at 2.50 and 44.00 , I use the following code

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

in which item.OtherFields["price"] is a object I convert it to a string and then decimal

but Math.round doesn't work, it only shows 2.5 and 44. Can someone help this

+7
source share
4 answers

Math.Round does just that - round.

To format the number, you can use .ToString(formatString) as follows:

 item.OtherFields["price"].ToString("0.00") 
+25
source

Use string formatting function

 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 */ 
+12
source

This should work for you.

 yourvalue.ToString ("0.00"); 
+4
source
 decimal dValue = 2.5; string sDisplayValue = dValue.ToString("0.00"); 
+3
source

All Articles