Converting string to double problem... (so simple)+

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
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:

Code:
double d = 0.0

d = Convert.ToDouble(textBox.Text)

What does this do? It converts the "7.5" to "75.0", and I want it to be 7.5!!!

Help? ;)
 
Last edited:
I was unable to duplicate this behavior. Could there by anything else that might be contributing to the problem?
 
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
 
Well, I created a new application containing the only code added by me as show bellow:

Code:
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?
 
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!
 
EFileTahi-A said:
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.
 
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 ;).
 
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...
 
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.
 
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 -
 
Back
Top