2

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);
1
  • 1
    Did you really mean a thousands-separator (,) or do you want a decimal point? Commented Sep 12, 2010 at 20:51

4 Answers 4

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

(tested version)

Sign up to request clarification or add additional context in comments.

4 Comments

This will an empty string for 0.
Well, it took me some time before I fired up Visual Studio, but now I did test it.
I find Mono's C# REPL comes in handy for quick testing like this.
@Matthew - Well, if I'd go that way, I'd might as well use a text file and the command prompt, as Jon Skeet says he does :) But otherwise it seems as an interesting app, thanks for the link.
2
String.Format("{0:#,0}", myDouble);

1 Comment

Yes, indeed. To my defense, I can say that I first checked in Visual Studio, and then when I came back to correct my answer I saw Matthew had posted his, but hmm, who would believe me...
1

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);

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.