integer, thousands separator, no decimal

what is the simplest String.Format template to format an integer with a thousands separator (comma) and no decimal places? en-US culture. Examples of desired formatting:

1200 = 1,200

900 = 900

8 = 8

thanks!


N0 is the format you are looking for. N will format it with commas, and the 0 is to tell it to not use decimals:

// will format as 33,540
string.Format("{0:N0}", 33540.54M)

// example using an integer: 1,200
string.Format("{0:N0}", 1200);

From MSDN:

Result: Integral and decimal digits, group separators, and a decimal separator with optional negative sign. Supported by: All numeric types. Precision specifier: Desired number of decimal places. Default precision specifier: Defined byNumberFormatInfo.NumberDecimalDigits. More information: The Numeric ("N") Format Specifier.


That's not possible to do using only a format string. You also have to specify the culture or number format, or make sure that the current culture is the right one. The thousands separator uses the separator specified in the culture, you can't specify the separator in the format.

The format string N0 is the shortest to get thousands separator and zero decimal places.

Example:

String.Format(CultureInfo.GetCultureInfo("en-US"), "{0:N0}", 12345678.901234)

我发现一个按预期工作:

{0:#,0}
链接地址: http://www.djcxy.com/p/50546.html

上一篇: String.Format()在数字中添加逗号,以使数字不起作用

下一篇: 整数,千位分隔符,无小数