我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
我想在一个数字的千位上加一个逗号。
String.Format()是正确的路径吗?我应该使用什么格式?
当前回答
更简单,使用字符串插值代替字符串。格式
$"{12456:n0}"; // 12,456
$"{12456:n2}"; // 12,456.00
或者使用yourVariable
double yourVariable = 12456.0;
$"{yourVariable:n0}";
$"{yourVariable:n2}";
其他回答
如果你想在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”
int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000
请注意,您正在格式化的值应该是数字。 它看起来不像会接受数字的字符串表示,格式是逗号。
就这么简单:
float num = 23658; // for example
num = num.ToString("N0"); // Returns 23,658
更多信息在这里