我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
当前回答
$"{1234:n}"; // Output: 1,234.00
$"{1234:n0}"; // No digits after the decimal point. Output: 9,876
其他回答
$"{1234:n}"; // Output: 1,234.00
$"{1234:n0}"; // No digits after the decimal point. Output: 9,876
请注意,您正在格式化的值应该是数字。 它看起来不像会接受数字的字符串表示,格式是逗号。
下面的示例显示了使用包含零占位符的自定义格式字符串格式化的几个值。
String.Format("{0:N1}", 29255.0);
Or
29255.0.ToString("N1")
结果“29255 .0
String.Format("{0:N2}", 29255.0);
Or
29255.0.ToString("N2")
结果“29255”
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
我尝试了上面的许多建议,但下面的建议更适合我:
string.Format("{0:##,###.00}", myValue)
但是当你有像0.2014这样的值时,它会失败,因为我使用。21
string.Format("{0:#,##0.00}", myValue)