我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
当前回答
就这么简单:
float num = 23658; // for example
num = num.ToString("N0"); // Returns 23,658
更多信息在这里
其他回答
您需要相同的格式值和特定于区域性。
Double value= 1234567;
value.ToString("#,#.##", CultureInfo.CreateSpecificCulture("hi-IN"));
输出:34567
如果你想在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
下面的示例显示了使用包含零占位符的自定义格式字符串格式化的几个值。
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”
这是最好的格式。适用于所有这些情况:
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
我不再担心文化和潜在的格式问题的方法是,我将其格式化为货币,然后去掉货币符号。
如果(小数。TryParse(tblCell, out result)
{
formattedValue = result.ToString("C").Substring(1);
}