How do I round a decimal value to 2 decimal places (for output on a page)


When displaying the value of a decimal currently with .ToString(), it's accurate to like 15 decimal places, and since I'm using it to represent dollars and cents, I only want the output to be 2 decimal places.

Do I use a variation of .ToString() for this?

8/21/2017 10:33:45 AM

Accepted Answer

decimalVar.ToString ("#.##"); // returns "" when decimalVar == 0


decimalVar.ToString ("0.##"); // returns "0"  when decimalVar == 0
2/9/2017 9:20:09 PM


This will:

  • Round off to 2 decimal places eg. 23.45623.46
  • Ensure that there are always 2 decimal places eg. 2323.00; 12.512.50

Ideal for displaying currency.

Check out the documentation on ToString("F") (thanks to Jon Schneider).


If you just need this for display use string.Format

String.Format("{0:0.00}", 123.4567m);      // "123.46"

The "m" is a decimal suffix. About the decimal suffix:


Given decimal d=12.345; the expressions d.ToString("C") or String.Format("{0:C}", d) yield $12.35 - note that the current culture's currency settings including the symbol are used.

Note that "C" uses number of digits from current culture. You can always override default to force necessary precision with C{Precision specifier} like String.Format("{0:C2}", 5.123d).


If you want it formatted with commas as well as a decimal point (but no currency symbol), such as 3,456,789.12...


There's already two high scoring answers that refer to Decimal.Round(...) but I think a little more explanation is needed - because there's an unexpected important property of Decimal that isn't obvious.

A Decimal 'knows' how many decimal places it has based upon where it came from

For instance the following may be unexpected :

Decimal.Parse("25").ToString()          =>   "25"
Decimal.Parse("25.").ToString()         =>   "25"
Decimal.Parse("25.0").ToString()        =>   "25.0"
Decimal.Parse("25.0000").ToString()     =>   "25.0000"

25m.ToString()                          =>   "25"
25.000m.ToString()                      =>   "25.000"

Doing the same operations with Double will give no decimal places ("25") for each of the above.

When you want a decimal to 2 decimal places theres about a 95% chance it's because it's currency in which case this is probably fine for 95% of the time:

Decimal.Parse("25.0").ToString("c")     =>   "$25.00"

Or in XAML you just use {Binding Price, StringFormat=c}

One case I ran into where I needed a decimal AS a decimal was when sending XML to Amazon's webservice. The service was complaining because a Decimal value (originally from SQL Server) was being sent as 25.1200 and rejected, (25.12 was the expected format).

All I needed to do was Decimal.Round(...) with 2 decimal places to fix the problem.

 // This is an XML message - with generated code by XSD.exe
 StandardPrice = new OverrideCurrencyAmount()
       TypedValue = Decimal.Round(product.StandardPrice, 2),
       currency = "USD"

TypedValue is of type Decimal so I couldn't just do ToString("N2") and needed to round it and keep it as a decimal.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]