I want to add a comma in the thousands place for a number. String.Format()
?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Below is a good solution in Java though!
or for a more robust way you may want to get the locale of a particular place, then use as below:
US Locale OUTPUT: $9,999,999.00
German Locale output
OUTPUT: 9.999.999,00 €
Indian Locale output
OUTPUT: Rs.9,999,999.00
Estonian Locale output
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
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.
Standard formats, with their related outputs,
Example output (en-us culture):
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
: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.just simple as this:
more info is in Here
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,00Note: Some cultures use
,
to mean decimal rather than.
so be careful.