|
-
July 15th, 2005, 05:31 AM
#1
Float conversion problem
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:
Code:
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
-
July 15th, 2005, 05:55 AM
#2
Re: Float conversion problem
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!
-
July 15th, 2005, 06:09 AM
#3
Re: Float conversion problem
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.
-
July 15th, 2005, 06:26 AM
#4
Re: Float conversion problem
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.
 Originally Posted by Anders
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" of your application according to the .Net framework. However, if you want a specific way, go as suggested by Anders.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 15th, 2005, 06:30 AM
#5
Re: Float conversion problem
Thank you. Now I am using
Code:
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
-
July 15th, 2005, 06:32 AM
#6
Re: Float conversion problem
 Originally Posted by exterminator
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 :-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|