Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display number to 2 decimal places in mvc3,C#? [duplicate]

Possible Duplicate:
Using String Format to show decimal upto 2 places or simple integer
How to set decimal point in 2 decimal places?

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());}
  [email protected](prolistprice, 2, MidpointRounding.AwayFromZero)

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

but Math.round is not working it shows 2.5 and 44 only.. Can anyone help this

like image 782
Prakash Avatar asked Nov 08 '12 08:11

Prakash


2 Answers

Math.Round does just that - round.

To format the number you may use .ToString(formatString) like so:

item.OtherFields["price"].ToString("0.00")
like image 187
PeteGO Avatar answered Sep 19 '22 03:09

PeteGO


Use string format 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
*/
like image 39
Naresh Pansuriya Avatar answered Sep 22 '22 03:09

Naresh Pansuriya