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 impressi
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.
I think:
string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 4.3);
should do what you want.
Use a CultureInfo that has commas instead of decimal point (French for instance) :
string.Format(CultureInfo.GetCultureInfo("fr-FR"), "{0:0.0}", 4.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.
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);
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;