Dennis
Dennis

Reputation: 905

C# formatting number

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.

Upvotes: 6

Views: 196

Answers (3)

Dennis
Dennis

Reputation: 905

Found the solution.

{0:#,#.##}

Upvotes: 6

Mario Vernari
Mario Vernari

Reputation: 7306

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.

Upvotes: 0

LaggKing
LaggKing

Reputation: 53

string stringNumber = number.ToString("#,##0");

Upvotes: 0

Related Questions