5

I need one C# formatting string that will handle those cases:

For input of 1234.561 should produce: 1,234.56

For input of 1234 should produce: 1,234

I tried {0:N0}, {0:N2} and {0:#.##}. Doesn't work.

CCovey
  • 799
  • 1
  • 10
  • 17
Dennis
  • 765
  • 5
  • 8
  • See http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number. Make sure the input is decimal/float or integer but NOT string! – Dukhabandhu Sahoo Dec 08 '13 at 06:12
  • Have seen this thread. Cant find anything that will fit the bill there. – Dennis Dec 08 '13 at 06:17

3 Answers3

5

Found the solution.

{0:#,#.##}

Dennis
  • 765
  • 5
  • 8
0
string stringNumber = number.ToString("#,##0");
LaggKing
  • 69
  • 7
0

Did you try with "{0:F2}"? I don't think you can use the "#" as pattern.

UPDATE: I was wrong. The "#" is allowed as custom placeholder.

Mario Vernari
  • 6,649
  • 1
  • 32
  • 44