0

String.Format to add commas in thousands place just add space without comma.

I tried:

 var total = string.Format("{0:n0}", 12345);
 // expect: total = 12,345
 // actual: total = 12 345

Anything I'm missing?

rethabile
  • 3,029
  • 6
  • 34
  • 68
  • Possible duplicate of [.NET String.Format() to add commas in thousands place for a number](https://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – Liam Jun 05 '17 at 09:57
  • 3
    You missed updating your SO profile so we can tell where you live. The exact formatting is culture-specific and can be overridden with Control Panel. Consider using CultureInfo.InvariantCulture.NumberFormat if you insist on commas. – Hans Passant Jun 05 '17 at 09:57
  • It depends on your current culture settings. – Renatas M. Jun 05 '17 at 10:00

2 Answers2

6

This is a culture thing. In your locale, space is the thousands specifier, apparently. I see commas. To see a specific locale's output, specify that explicitly. A very common option is "invariant":

var total = string.Format(CultureInfo.InvariantCulture, "{0:n0}", 12345);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

This seems to be your regional settings on you PC:

enter image description here

The DEMO works fine

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27