How do I display a decimal value to 2 decimal places?

wows picture wows · Oct 3, 2008 · Viewed 1.1M times · Source

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?

Answer

albertein picture albertein · Oct 3, 2008
decimalVar.ToString ("#.##"); // returns "" when decimalVar == 0

or

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