【发布时间】:2010-09-11 11:17:15
【问题描述】:
我想在数字的千位中添加一个逗号。
String.Format() 是正确的选择吗?我会使用什么格式?
【问题讨论】:
我想在数字的千位中添加一个逗号。
String.Format() 是正确的选择吗?我会使用什么格式?
【问题讨论】:
String.Format("{0:n}", 1234); // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876
【讨论】:
CurrentCulture像这个例子:var nlBE = new System.Globalization.CultureInfo("nl-BE"); nlBE.NumberFormat.CurrencyDecimalDigits = 2; nlBE.NumberFormat.CurrencyDecimalSeparator = ","; nlBE.NumberFormat.CurrencyGroupSeparator = "."; System.Threading.Thread.CurrentThread.CurrentCulture = nlBE;
string.Format("{0:n0}", Double.Parse(yourValue));
n 说明符的行为,但数字本身中存在尽可能多的十进制数字,我不得不使用此格式字符串而不是 #,##0.##(来自另一个答案here )
我发现这是最简单的方法:
myInteger.ToString("N0")
【讨论】:
int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000
【讨论】:
, 以外的字符作为千位分隔符,例如空格甚至 .。
.),.NET 将自动用该分隔符替换逗号。如果您仍然不明白这是为什么,我再次敦促您阅读 Roger 发布的链接。
如果你想要特定文化,你可能想试试这个:
(19950000.0).ToString("N",new CultureInfo("en-US")) = 19,950,000.00
(19950000.0).ToString("N",new CultureInfo("is-IS")) = 19.950.000,00
注意:有些文化使用, 表示十进制而不是.,所以要小心。
【讨论】:
标准格式及其相关输出,
Console.WriteLine("Standard Numeric Format Specifiers");
String s = String.Format("(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
- 1234, -1234.565F);
Console.WriteLine(s);
示例输出(en-us 文化):
(C) Currency: . . . . . . . . ($1,234.00)
(D) Decimal:. . . . . . . . . -1234
(E) Scientific: . . . . . . . -1.234565E+003
(F) Fixed point:. . . . . . . -1234.57
(G) General:. . . . . . . . . -1234
(default):. . . . . . . . -1234 (default = 'G')
(N) Number: . . . . . . . . . -1,234.00
(P) Percent:. . . . . . . . . -123,456.50 %
(R) Round-trip: . . . . . . . -1234.565
(X) Hexadecimal:. . . . . . . FFFFFB2E
【讨论】:
这是最好的格式。适用于所有这些情况:
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
【讨论】:
投票最多的答案很棒,并且已经帮助了大约 7 年。随着 C# 6.0 的引入,特别是字符串插值的引入,有一种更简洁、更安全的方式来执行 to add commas in thousands place for a number 所要求的操作:
var i = 5222000;
var s = $"{i:n} is the number"; // results to > 5,222,000.00 is the number
s = $"{i:n0} has no decimal"; // results to > 5,222,000 has no decimal
变量i 代替占位符(即{0})。所以没有必要记住哪个物体去哪个位置。格式(即:n)没有改变。有关新功能的完整功能,您可以go to this page。
【讨论】:
【讨论】:
String.Format("{0:#,###,###.##}", MyNumber)
这将在相关点给你逗号。
【讨论】:
以下示例显示了几个使用包含零占位符的自定义格式字符串格式化的值。
String.Format("{0:N1}", 29255.0);
或者
29255.0.ToString("N1")
结果“29,255.0”
String.Format("{0:N2}", 29255.0);
或者
29255.0.ToString("N2")
结果“29,255.00”
【讨论】:
如果您希望强制使用“,”分隔符而不管文化(例如在跟踪或日志消息中),以下代码将起作用,并且具有告诉下一个偶然发现它的人您是什么的额外好处做。
int integerValue = 19400320;
string formatted = string.Format(CultureInfo.InvariantCulture, "{0:N0}", integerValue);
设置为“19,400,320”
【讨论】:
C# 7.1(可能更早?)通过字符串插值使这变得简单而美观:
var jackpot = 1_000_000; // underscore separators in numeric literals also available since C# 7.0
var niceNumberString = $"Jackpot is {jackpot:n}";
var niceMoneyString = $"Jackpot is {jackpot:C}";
【讨论】:
更简单,使用字符串插值代替 String.Format
$"{12456:n0}"; // 12,456
$"{12456:n2}"; // 12,456.00
或者使用你的变量
double yourVariable = 12456.0;
$"{yourVariable:n0}";
$"{yourVariable:n2}";
【讨论】:
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
【讨论】:
例如String.Format("{0:0,0}", 1);返回01,对我来说无效
这对我有用
19950000.ToString("#,#", CultureInfo.InvariantCulture));
输出 19,950,000
【讨论】:
请注意,您要格式化的值应该是数字。 看起来它不会采用数字的字符串表示形式,格式是逗号。
【讨论】:
String.Format("0,###.###"); also works with decimal places
【讨论】:
您可以使用这样的函数来格式化数字,并可选择传入所需的小数位。如果没有指定小数位,它将使用两个小数位。
public static string formatNumber(decimal valueIn=0, int decimalPlaces=2)
{
return string.Format("{0:n" + decimalPlaces.ToString() + "}", valueIn);
}
我使用十进制,但您可以将类型更改为任何其他类型或使用匿名对象。您还可以添加对负小数位值的错误检查。
【讨论】:
您想要相同的格式值和特定的文化。
Double value= 1234567;
value.ToString("#,#.##", CultureInfo.CreateSpecificCulture("hi-IN"));
输出:12,34,567
【讨论】:
我尝试了上面的许多建议,但以下对我来说效果更好:
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
【讨论】:
我过去不再担心文化和潜在格式问题的方法是我将其格式化为货币,然后取出货币符号。
if (decimal.TryParse(tblCell, out result))
{
formattedValue = result.ToString("C").Substring(1);
}
【讨论】:
fr-FR),或者使用多个字符来表示货币(例如da-DK),或者不分开数千人使用逗号(例如欧洲大陆的大部分地区)。