开发者

c# string format

开发者 https://www.devze.com 2023-01-16 08:25 出处:网络
How do I get the output of this to read 0 instead of 00 when th开发者_开发百科e value is 0? String.Format(\"{0:0,0}\", myDouble);

How do I get the output of this to read 0 instead of 00 when th开发者_开发百科e value is 0?

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


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

(tested version)


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消