String.Format() to add commas in thousands place for a number not working

String.Format to add commas in thousands place just add space without comma.

I tried:

 var total = string.Format("{0:n0}", 12345);
 // expect: total = 12,345
 // actual: total = 12 345

Anything I'm missing?


This is a culture thing. In your locale, space is the thousands specifier, apparently. I see commas. To see a specific locale's output, specify that explicitly. A very common option is "invariant":

var total = string.Format(CultureInfo.InvariantCulture, "{0:n0}", 12345);

This seems to be your regional settings on you PC:

在这里输入图像描述

The DEMO works fine

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

上一篇: 美元($)符号和逗号以数字为单位,String.Format()

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