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

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


当前回答

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

其他回答

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

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

或者使用yourVariable

 double yourVariable = 12456.0;
 $"{yourVariable:n0}"; 
 $"{yourVariable:n2}"; 
$"{1234:n}";  // Output: 1,234.00
$"{1234:n0}"; // No digits after the decimal point. Output: 9,876
String.Format("{0:#,###,###.##}", MyNumber)

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

例如String。格式(“{0时0}",1);返回01,对我来说无效

这对我很有用

19950000.ToString("#,#", CultureInfo.InvariantCulture));

输出 19950000年

请注意,您正在格式化的值应该是数字。 它看起来不像会接受数字的字符串表示,格式是逗号。