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

I know this is an old question, but I was surprised to see that no one seemed to post an answer that;

  1. Didn't use bankers rounding
  2. Didn't keep the value as a decimal.

This is what I would use:

decimal.Round(yourValue, 2, MidpointRounding.AwayFromZero);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow