Search code examples
c#decimalcurrencytostring

c# Decimal to string for currency


To display a currency we do:

ToString("0.##")

For value 5.00 the output is:

5

For value 5.98 the output is:

5.98

For value 5.90 the output is:

5.9

I need the third case to come out with 2 decimal points, eg:

5.90

How can I do this without it affecting the other results?


Solution

  • I know this doesn't give you a format that fixes the problem, but it's a simple solution to work around it.

    (5.00).ToString("0.00").Replace(".00","");  // returns 5
    (5.90).ToString("0.00").Replace(".00", ""); // returns 5.90
    (5.99).ToString("0.00").Replace(".00", ""); // returns 5.99