.NET String.Format() to add commas in thousands pl

2018-12-31 03:08发布

I want to add a comma in the thousands place for a number. String.Format()?

标签: c# .net
20条回答
低头抚发
2楼-- · 2018-12-31 03:21

Below is a good solution in Java though!

NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println(fmt.format(n));

or for a more robust way you may want to get the locale of a particular place, then use as below:

int n=9999999;
Locale locale = new Locale("en", "US");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
System.out.println(fmt.format(n));

US Locale OUTPUT: $9,999,999.00

German Locale output

Locale locale = new Locale("de", "DE");

OUTPUT: 9.999.999,00 €

Indian Locale output

Locale locale = new Locale("de", "DE");

OUTPUT: Rs.9,999,999.00

Estonian Locale output

Locale locale = new Locale("et", "EE");

OUTPUT: 9 999 999 €

As you can see in different outputs you don't have to worry about the separator being a comma or dot or even space you can get the number formatted according to the i18n standards

查看更多
皆成旧梦
3楼-- · 2018-12-31 03:23

Note that the value that you're formatting should be numeric. It doesn't look like it will take a string representation of a number and format is with commas.

查看更多
春风洒进眼中
4楼-- · 2018-12-31 03:25

Standard formats, with their related outputs,

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);

Example output (en-us culture):

(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
查看更多
流年柔荑漫光年
5楼-- · 2018-12-31 03:26

The most voted answer was great and has been helpful for about 7 years. With the introduction of C# 6.0 and specifically the String Interpolation there's a neater and, IMO safer, way to do what has been asked 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

Where the variable i is put in place of the placeholder (i.e. {0}). So there's no need to remember which object goes to which position. The formatting (i.e. :n) hasn't changed. For a complete feature of what's new, you may go to this page.

查看更多
心情的温度
6楼-- · 2018-12-31 03:27

just simple as this:

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

more info is in Here

查看更多
妖精总统
7楼-- · 2018-12-31 03:28

If you want culture specific, you might want to try this:

(19950000.0).ToString("N",new CultureInfo("en-US")) = 19,950,000.00

(19950000.0).ToString("N",new CultureInfo("is-IS")) = 19.950.000,00

Note: Some cultures use , to mean decimal rather than . so be careful.

查看更多
登录 后发表回答