我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
当前回答
我尝试了上面的许多建议,但下面的建议更适合我:
string.Format("{0:##,###.00}", myValue)
但是当你有像0.2014这样的值时,它会失败,因为我使用。21
string.Format("{0:#,##0.00}", myValue)
其他回答
如果你想在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
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
您需要相同的格式值和特定于区域性。
Double value= 1234567;
value.ToString("#,#.##", CultureInfo.CreateSpecificCulture("hi-IN"));
输出:34567
更简单,使用字符串插值代替字符串。格式
$"{12456:n0}"; // 12,456
$"{12456:n2}"; // 12,456.00
或者使用yourVariable
double yourVariable = 12456.0;
$"{yourVariable:n0}";
$"{yourVariable:n2}";
例如String。格式(“{0时0}",1);返回01,对我来说无效
这对我很有用
19950000.ToString("#,#", CultureInfo.InvariantCulture));
输出 19950000年