add commas using String.Format for number and

Using String.Format how can i ensure all numbers have commas after every 3 digits eg 23000 = "23,000" and that 0 returns "0".

String.Format("{0:n}", 0); //gives 0.00 which i dont want. I dont want any decimal places, all numbers will be integers.


You can do this, which I find a bit cleaner to read the intent of:

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

Example:

string.Format("{0:#,###0}", 123456789); // 123,456,789
string.Format("{0:#,###0}", 0); // 0

If your current culture seting uses commas as thousands separator, you can just format it as a number with zero decimals:

String.Format("{0:N0}", 0)

Or:

0.ToString("N0")

from msdn

double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));

Displays 1,234,567,890

链接地址: http://www.djcxy.com/p/50538.html

上一篇: 格式编号在C#中

下一篇: 使用String.Format为数字和。添加逗号