Inside Man
Inside Man

Reputation: 4372

String Format for up to 2 decimal places or simple integer and with thousand separator - C#

Here is a string format which formats a number up to 2 decimal places and if there are no decimal places then it will show the simple integer itself:

String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.0);         // "123"

so {0:0.##} will do the job. But I need to have a thousand separator on integer part. so something like 1234.456 should be like 1,234.45. I know {0:n0} will do a thousand separators but how to combine it with the decimal place formatter?

Upvotes: 0

Views: 2587

Answers (1)

GSerg
GSerg

Reputation: 78155

String.Format("{0:#,##0.##}", 123.0);

Upvotes: 4

Related Questions