24

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?

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • Any reason you can't use the currency format string? `value.ToString("c")`? – Steve Danner May 03 '12 at 18:41
  • 4
    Why not simply do `someDecimal.ToString("c", new CultureInfo("en-US"))`? It then also applies the currency symbol. – Tejs May 03 '12 at 18:41
  • @Tejs we're writing our own store and it supports multiple currencies, that wont fit in with the design very well unfortunately at this stage! – Tom Gullen May 03 '12 at 18:42
  • 2
    I don't think you can do that with any built-in or custom format strings (selectively either doing no decimal point or two decimal points). You may have to add logic to select one or the other or implement your own `IFormatProvider`. – Jacob May 03 '12 at 18:43
  • @Jacob that's what I was thinking as well, just going to be a PITA to do at this stage! – Tom Gullen May 03 '12 at 18:44
  • @TomGullen - Supporting multiple currencies isn't a problem, that's what the `new CultureInfo("en-US")` does. Try it with `new CultureInfo("en-GB")` or `new CultureInfo("fr-FR")`. – Justin Morgan - On strike Jul 07 '14 at 16:42

6 Answers6

26

Try:

value.ToString("#,##0.00")

Or just:

value.ToString("C")

I know of no built-in way to expand out all two decimal places only when both are not zero. Would probably just use an if statement for that.

if (s.EndsWith(".00"))
    s = s.Substring(0, s.Length - 3);
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    The OP says that he wants the decimal portion to be omitted if it's an integer value. – Jacob May 03 '12 at 18:44
18

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
Zachary
  • 6,522
  • 22
  • 34
11

You could use an extension method, something like this:

public static string ToCurrencyString(this decimal d)
{
    decimal t = Decimal.Truncate(d);
    return d.Equals(t) ? d.ToString("0.##") : d.ToString("#, ##0.00")
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Clark
  • 376
  • 1
  • 6
10

# means if there is no number leave it empty 0 means put a 0 if there is no number

ToString("0.00")

ahaliav fox
  • 2,217
  • 22
  • 21
1

Not sure if I'm missing something, but can't you just do this:

decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
gcoleman0828
  • 1,541
  • 3
  • 30
  • 49
0

Just use myDecimal.ToString("N"),

With "N" parameter will transform decimal to string, and the number of decimals to show will be defined by your SO settings.

https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimaldigits(v=vs.110).aspx

Marc Cals
  • 2,963
  • 4
  • 30
  • 48