Kory
Kory

Reputation: 1446

c# string format

How do I get the output of this to read 0 instead of 00 when the value is 0?

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

Upvotes: 2

Views: 686

Answers (4)

Henk Holterman
Henk Holterman

Reputation: 273244

While the posted answers here ("{0:#,0}") are correct I would strongly suggest using a more readable picture (also to avoid confusion about decimal/thousand separators):

string.Format("{0:#,##0}", v);     // to print 1,234
string.Format("{0:#,##0.00}", v);  // to print 1,234.56

But all those pictures work the same, including 2 comma's for 1e6 etc.

Upvotes: 1

Dan Dumitru
Dan Dumitru

Reputation: 5423

string.Format("{0:#,0}", myDouble);

(tested version)

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 838186

Another alternative is to use this:

string s = string.Format("{0:n0}", myDouble);

If you always want commas as the thousands separator and not to use the user's locale then use this instead:

string s = myDouble.ToString("n0", CultureInfo.InvariantCulture);

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

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

Upvotes: 2

Related Questions