我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
当前回答
$"{1234:n}"; // Output: 1,234.00
$"{1234:n0}"; // No digits after the decimal point. Output: 9,876
其他回答
如果你想要特定文化,你可能想试试这个:
使用命名空间:"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
注意:一些区域性使用,表示小数而不是。所以要小心。
您需要相同的格式值和特定于区域性。
Double value= 1234567;
value.ToString("#,#.##", CultureInfo.CreateSpecificCulture("hi-IN"));
输出:34567
这是最好的格式。适用于所有这些情况:
String.Format( "{0:#,##0.##}", 0 ); // 0
String.Format( "{0:#,##0.##}", 0.5 ); // 0.5 - some of the formats above fail here.
String.Format( "{0:#,##0.##}", 12314 ); // 12,314
String.Format( "{0:#,##0.##}", 12314.23123 ); // 12,314.23
String.Format( "{0:#,##0.##}", 12314.2 ); // 12,314.2
String.Format( "{0:#,##0.##}", 1231412314.2 ); // 1,231,412,314.2
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
例如String。格式(“{0时0}",1);返回01,对我来说无效
这对我很有用
19950000.ToString("#,#", CultureInfo.InvariantCulture));
输出 19950000年