Click to See Complete Forum and Search --> : Float conversion problem


gbr
July 15th, 2005, 05:31 AM
Hi,

as I found out C# does conversion of float strings in different ways on different OS (e.g. english WindowsXP and german WindowsXP).

For example if I execute:

string st = "100.00";
float fl = (float)System.Convert.ToDouble(st);
MessageBox.Show( st + "-->" + fl.ToString() );

st = "37,00";
fl = (float)System.Convert.ToDouble(st);
MessageBox.Show( st + "-->" + fl.ToString() );


I get "10000" and "37" for the converted floats on the german OS. However if I execute the same stripped of code on an english Windows it converts to "100" and "3700".
I can imagine what went wrong, but I don't know how to fix that issue. The question is: how to tell the application what to use as a float separator no matter what kind of language is used, because one can assume that internally all float strings are handled in "12.345" format.

Thanks in advance,
gbr

Anders
July 15th, 2005, 05:55 AM
Your question is excellently answered by MSDN. Look up System.Convert class and read about System.Convert.ToDouble(string, IFormatProvider). In short it says that you can pass an instance of NumberFormatInfo as IFormatProvider. NumberFormatInfo has members such as NumberDecimalSeparator.

HTH!

Anders
July 15th, 2005, 06:09 AM
Oh, and by the way, I don't think different languages of XP is the issue, rather it is differences in Regional Settings. When you are converting user input you should use the overload of ToDouble in your post, not the one I suggested, because as I understand it your overload uses formatting info from Regional Settings. This way the user has the option to choose any format he/she likes of floating numbers and you will be able to convert them successfully.
Cheers.

exterminator
July 15th, 2005, 06:26 AM
Actually, its a bit wierd for me. The convert should basically fail because there's a comma (,) in the string !! Anyways, what Anders says is correct.I don't think different languages of XP is the issue, rather it is differences in Regional Settings.The regional setting are there in the windows registry. It picks up the basic formats from there - for dates, currencies etc. Try using the overloads specifying the formats. By the way, it makes sense, since you are using a German Version of the OS..the inputs you would be providing would be as per your regional settings. So, in a way that is correct and part of "Localization (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpqstart/html/cpsmpnetsamples-aspnetlocalization.asp)" of your application according to the .Net framework. However, if you want a specific way, go as suggested by Anders. :thumb:

gbr
July 15th, 2005, 06:30 AM
Thank you. Now I am using

System.Globalization.NumberFormatInfo nfi =
new System.Globalization.CultureInfo( "en-US", false ).NumberFormat;
fl = (float)System.Convert.ToDouble( st, nfi );

which converts to the same float without caring about region settings.

gbr

Anders
July 15th, 2005, 06:32 AM
The convert should basically fail because there's a comma (,) in the string !!

Not neccessarily, as I understand it. My current Regional Settings has a comma as the grouping character and thus it is a legal characters that is just discarded when converting. I think. Not sure :-)