我想在一个数字的千位上加一个逗号。

String.Format()是正确的路径吗?我应该使用什么格式?


当前回答

int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));

其他回答

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

这将在相关点处给出逗号。

您需要相同的格式值和特定于区域性。

 Double value= 1234567;
 value.ToString("#,#.##", CultureInfo.CreateSpecificCulture("hi-IN"));

输出:34567

String.Format("0,###.###"); also works with decimal places

就这么简单:

float num = 23658; // for example 
num = num.ToString("N0"); // Returns 23,658

更多信息在这里

更简单,使用字符串插值代替字符串。格式

 $"{12456:n0}"; // 12,456
 $"{12456:n2}"; // 12,456.00

或者使用yourVariable

 double yourVariable = 12456.0;
 $"{yourVariable:n0}"; 
 $"{yourVariable:n2}";