Formatting Numbers as Strings with Commas in place of Decimals

点点圈 提交于 2019-12-28 04:13:22

问题


I have the following number: 4.3

I'd like to display this number as 4,3 for some of our European friends.

I was under the impression that the following line would do the trick:

string ret = string.Format("{0:0,0}", 4.3); // returns "04", not "4,3"

Am I using the incorrect string?


回答1:


I think:

string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 4.3); 

should do what you want.




回答2:


NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = ".";

double num = 4.3;
string ret = num.ToString(nfi);    // 4,3



回答3:


This depends on what you want to do. If you want to make your entire application ready for some other language output just use

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo ("de-DE");

If you want to have a mixed language application (that means some of the outputs are formatted in one language some in another) you have to use the overloads of the specific String functions like:

var culture =  System.Globalization.CultureInfo.GetCultureInfo("de-DE");   
String.Format (culture, "{0:0.0}", 4.3);

The first method is preferred if it is possible because you only need one single line in your application and everything will be as expected. In the second you have to add the culture parameter to every String ouput method in your entire application.




回答4:


For a more general solution, you can set the thread's UI culture to the culture used by your European friends. This will affect the presentation of numbers, currency, dates, etc.




回答5:


The number formatting by default uses the NumberFormatInfo from the current CultureInfo, so that it displays using the regional settings on the computer.

Therefore, you don't really need to do anything special about it, except maybe making sure that the correct CultureInfo is being used.

As for the question, yes the string is invalid. A "," denotes a thousand-separator, not the decimal-separator. Have a look the the NumberFormatInfo and the custom number formats.




回答6:


Use a CultureInfo that has commas instead of decimal point (French for instance) :

string.Format(CultureInfo.GetCultureInfo("fr-FR"), "{0:0.0}", 4.3);



回答7:


Yes, you are using the wrong string, and also the problem can't be solved by only providing a formatting string.

What your formatting string does is to format the number using the pattern "0", then aligned to the length 0.

When you specify the decimal separator in a formatting string it's always a period regardless of the current culture. The decimal separator in the result of the formatting on the other hand is always the one used by the current culture. So, to get a comma as decimal separator in the result you have to make sure that the culture used for the formatting is one that uses comma as decimal separator.

You can either set the current culture for the thread so that it's used by default by the formatting, or specify a culture in the call:

string ret = String.Format(CultureInfo.GetCultureInfo(1053), "{0:0.0}", 4.3);



回答8:


Settings the thread culture will do this conversion automatically. However, if you want to format this with a custom string you can do the following:

string ret = string.Format("{0:0.00}", 4.3);

or

string ret = (4.3f).ToString("0.00");

The "." is the format specifier for decimal point for the current culture. More info for custom number formats are found at: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

You can always check what character for the decimal point is with:

string decimalChar = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;



回答9:


Number formatting can be handled for you by the framework if you use the correct culture when manipulating the number.

Console.WriteLine(4.3);

Console.WriteLine(4.3.ToString(CultureInfo.GetCultureInfo("fr-fr")));

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-fr");
Console.WriteLine(4.3);

If you want to do a "one-off" display, the second approach will work. If you want to display every number correctly, you really should be setting the current thread's culture. That way, any number manipulation will handle decimal separators, grouping characters and any other culture-specific things correctly.

The same goes for parsing numbers. If a user enters 1,234, how do you know whether they have entered 1.234 (the comma being the decimal separator) or 1234 (the comma being a grouping separator)? This is where the culture helps out as it knows how to display numbers and can also be used to parse them correctly:

Console.WriteLine(double.Parse("1,234"));

Console.WriteLine(double.Parse("1,234", CultureInfo.GetCultureInfo("fr-fr")));

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-fr");
Console.WriteLine(double.Parse("1,234"));

The above will output 1234 (the comma is a decimal separator in my en-us default culture), 1.234 (the comma is a decimal separator in French) and 1,234 (again, the comma is a decimal separator in French, and also the thread's culture is set To French so it displays using this culture - hence the comma as a decimal separator in the output).



来源:https://stackoverflow.com/questions/1559185/formatting-numbers-as-strings-with-commas-in-place-of-decimals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!