Possible duplicate:Using String Format to display decimal to 2 places or a simple integerHow 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
2.5 and 44.
2.50 and 44.00
@{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
item.OtherFields["price"] is a object
but Math.round doesn't work, it only shows 2.5 and 44. Can someone help this
Math.Round does just that - round.
Math.Round
To format the number, you can use .ToString(formatString) as follows:
.ToString(formatString)
item.OtherFields["price"].ToString("0.00")
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 */
This should work for you.
yourvalue.ToString ("0.00");
decimal dValue = 2.5; string sDisplayValue = dValue.ToString("0.00");