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

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


当前回答

就这么简单:

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

更多信息在这里

其他回答

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

我不再担心文化和潜在的格式问题的方法是,我将其格式化为货币,然后去掉货币符号。

如果(小数。TryParse(tblCell, out result)

{
  formattedValue = result.ToString("C").Substring(1);
}
$"{1234:n}";  // Output: 1,234.00
$"{1234:n0}"; // No digits after the decimal point. Output: 9,876

我发现这是最简单的方法:

myInteger.ToString("N0")

如果你想要特定文化,你可能想试试这个:

使用命名空间:"using System.Globalization;"

(19950000.0)。ToString("N",new CultureInfo("en-US")) = 19,950,000.00

(19950000.0)。ToString("N",new CultureInfo("is-IS")) = 19.950.000,00

印度文化: (19950000.0)。ToString("N",new CultureInfo("hi-IN"))= 1,99,50,000.00

注意:一些区域性使用,表示小数而不是。所以要小心。