Advertisement
Advertisement


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


Question

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?

2017/08/21
1
672
8/21/2017 10:33:45 AM

Accepted Answer

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

or

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


decimalVar.ToString("F");

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).

2020/04/28

If you just need this for display use string.Format

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

http://www.csharp-examples.net/string-format-double/

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

http://msdn.microsoft.com/en-us/library/364x0z75.aspx

2013/09/10

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).

2014/01/30

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

decimalVar.ToString("n2");
2009/04/21

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.

2020/07/15

Source: https://stackoverflow.com/questions/164926
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]