. NET String. Format()在数千个位置为数字添加逗号

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

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

889568 次浏览
String.Format("{0:n}", 1234);  // Output: 1,234.00String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876
String.Format("{0:#,###,###.##}", MyNumber)

这将在相关点处为您提供逗号。

请注意,要格式化的值应该是数字。它看起来不像是一个数字的字符串表示,格式是逗号。

int number = 1000000000;string whatYouWant = number.ToString("#,##0");//You get: 1,000,000,000
int num = 98765432;Console.WriteLine(string.Format("{0:#,#}", num));

我发现这是最简单的方法:

myInteger.ToString("N0")

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

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

注意:一些文化使用,表示十进制而不是.,所以要小心。

我以前不再担心文化和潜在的格式问题的方法是,我将其格式化为货币,然后去掉货币符号。

if (decimal.TryParse(tblCell, out result))

{formattedValue = result.ToString("C").Substring(1);}

例如String.Format("{0:0,0}", 1);返回01,对于我来说是无效的

这对我有用

19950000.ToString("#,#", CultureInfo.InvariantCulture));

输出19,950,000

标准格式及其相关输出,

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

如果您希望强制使用“,”分隔符而不管文化(例如在跟踪或日志消息中),以下代码将起作用,并且具有告诉下一个偶然发现它的人您正在做什么的额外好处。

int integerValue = 19400320;string formatted = string.Format(CultureInfo.InvariantCulture, "{0:N0}", integerValue);

设置格式为“19,400,320”

如果你想在DataGridview中显示它,你应该更改它的类型,因为默认值是String,并且由于你将其更改为小数,它认为是带浮点数的数字

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

您可以使用这样的函数来格式化数字并可选择传入所需的小数位。如果未指定小数位,它将使用两位小数位。

    public static string formatNumber(decimal valueIn=0, int decimalPlaces=2){return string.Format("{0:n" + decimalPlaces.ToString() + "}", valueIn);}

我使用decimal,但您可以将类型更改为任何其他类型或使用匿名对象。您还可以添加错误检查以检查负小数位值。

这是最好的格式。适用于所有这些情况:

String.Format( "{0:#,##0.##}", 0 ); // 0String.Format( "{0:#,##0.##}", 0.5 ); // 0.5 - some of the formats above fail here.String.Format( "{0:#,##0.##}", 12314 ); // 12,314String.Format( "{0:#,##0.##}", 12314.23123 ); // 12,314.23String.Format( "{0:#,##0.##}", 12314.2 ); // 12,314.2String.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 numbers = $"{i:n0} has no decimal"; // results to > 5,222,000 has no decimal

其中变量i代替占位符(即{0})。因此无需记住哪个对象去了哪个位置。格式(即:n)没有改变。对于新功能的完整功能,您可以转到此页

更简单,使用字符串插值而不是String. Format

 $"{12456:n0}"; // 12,456$"{12456:n2}"; // 12,456.00

或使用您的变量

 double yourVariable = 12456.0;$"{yourVariable:n0}";$"{yourVariable:n2}";

以下示例显示使用包含零占位符的自定义格式字符串格式化的几个值。

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”

就这么简单:

float num = 23658; // for examplenum = num.ToString("N0"); // Returns 23,658

更多信息在这里

String.Format("0,###.###"); also works with decimal places

C#7.1(也许更早?)使用字符串插值使其变得简单且美观:

var jackpot = 1_000_000; // underscore separators in numeric literals also available since C# 7.0var niceNumberString = $"Jackpot is {jackpot:n}";var niceMoneyString = $"Jackpot is {jackpot:C}";

您需要相同的格式值和特定的区域性。

 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)

试试这个:

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

结果是:"123,456,789"