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

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


当前回答

试试这个:

var number = 123456789;
var str = number.ToString("N0");

结果是:“123,456,789”

其他回答

$"{1234:n}";  // Output: 1,234.00
$"{1234:n0}"; // No digits after the decimal point. Output: 9,876

如果你想在DataGridview中显示它,你应该改变它的类型,因为默认是字符串,因为你把它改变为十进制,它认为是数字与浮点数

Dim dt As DataTable = New DataTable
dt.Columns.Add("col1", GetType(Decimal))
dt.Rows.Add(1)
dt.Rows.Add(10)
dt.Rows.Add(2)

DataGridView1.DataSource = dt

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

使用命名空间:"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

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

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

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

我尝试了上面的许多建议,但下面的建议更适合我:

string.Format("{0:##,###.00}", myValue)

但是当你有像0.2014这样的值时,它会失败,因为我使用。21

string.Format("{0:#,##0.00}", myValue)