EFileTahi-A Posted May 5, 2006 Posted May 5, 2006 (edited) Ok, what I want to do is so simple that I can't do it... I have a text box containing the value "7.5" which I need convert it to a double value: double d = 0.0 d = Convert.ToDouble(textBox.Text) [/Code] What does this do? It converts the "7.5" to "75.0", and I want it to be 7.5!!! Help? ;) Edited May 5, 2006 by EFileTahi-A Quote
mskeel Posted May 5, 2006 Posted May 5, 2006 I was unable to duplicate this behavior. Could there by anything else that might be contributing to the problem? Quote
*Experts* Nerseus Posted May 5, 2006 *Experts* Posted May 5, 2006 If that's really what's happening, it sounds like the CultureInfo somewhere is defining the "." period as a thousands separator instead of the decimal point. So maybe it thinks "7.5" looks like "7,5" which to me is still stupid, but maybe what you're seeing? -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
EFileTahi-A Posted May 5, 2006 Author Posted May 5, 2006 Well, I created a new application containing the only code added by me as show bellow: private void Form1_Load(object sender, System.EventArgs e) { string s = "7.5"; double d = Convert.ToDouble(s); } And it haves the same problem... So, if what you said Nerseus is true, how will I walk around this prob? Quote
EFileTahi-A Posted May 5, 2006 Author Posted May 5, 2006 Ok, it seems your theory Nerseus was right I changed my regional settings in windows and switched the decimal seperator from "," to "." and it worked fine... So, this also means that my application will have problems on certain OS... I hate this! Quote
Cags Posted May 5, 2006 Posted May 5, 2006 I cannot replicate this behavious either, what do you get if you use Double.Parse(s); Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted May 5, 2006 Leaders Posted May 5, 2006 So, this also means that my application will have problems on certain OS... It shouldn't. This should, if anything, prevent problems, provided that your users have set their regional settings properly through Windows. This is normally done when Windows is installed. A prompt comes up and asks you this information, so it is kind of hard to forget or neglect to do. When a user has their regional properties configured correctly, they should be able to type the number in as they normally would, and it should parse correctly. For example, if a French user types in "5.432,1" he would expect it to be parsed as five-thousand four-hundred thirty-two and one tenth, and provided that he has Windows property configured, it would be so parsed. And when I type "5,432.1" in, I should get the same result because I know my regional settings are correct. If your application requires portability between different computers and you need culture-neutral parsing there are different overloads for the .Parse methods that let you specify a culture or formatting details. Check out the relevant MSDN docs. Quote [sIGPIC]e[/sIGPIC]
Wile Posted May 6, 2006 Posted May 6, 2006 System.Globalization.CultureInfo.InvariantCulture is your friend for parsing. This is always fixed to be associated with the English language (no country/region specification). It uses the english definition of . and , for decimals. A tip: Parsing a string to a value without using a culture is (was?) detected by fxcop. I've seen lots of warnings on my own code for that ;). I dont agree with all fxcop warnings, but this one is pretty usefull if you live in a country where the decimals/thousand separator are different than in English ;). Quote Nothing is as illusive as 'the last bug'.
EFileTahi-A Posted May 7, 2006 Author Posted May 7, 2006 The problem is that I have windows in Portuguese and English languages at my job (because this is needed) where my application will run... Wile, if it is english fixed why do I have this problem? PS: Getting trully confused... Quote
Wile Posted May 7, 2006 Posted May 7, 2006 What I ment was that System.Globalization.CultureInfo.InvariantCulture is fixed. However if you dont specify a culture when converting, .NET will probably take the local settings of the machine, which are not fixed ;). The double.Parse() method has several overloads. Two of these let you specify an IFormatProvider. This IFormatProvider will tell the parse method what the decimal character is. If you use the invariantculture as IFormatProvider, you always have the same conversion, independent on the system settings. So [CS]double.Parse(myDouble)[/CS] would be depending on the local settings off the system but [CS]double.Parse(myDouble, System.Globalization.CultureInfo.InvariantCulture)[/CS] would always work the same way, no matter what the system settings are. Quote Nothing is as illusive as 'the last bug'.
EFileTahi-A Posted May 8, 2006 Author Posted May 8, 2006 Ah... Now it works ok! I think I will put your Avatar in a frame right on my desk Wile! ;) Thank you all who posted here. It's still good to know there are persons who are willing to help. - THREAD RESOLVED - Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.