Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by EFileTahi-A
  • *Experts*
Posted

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

"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
Posted

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?

Posted

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!

Posted
I cannot replicate this behavious either, what do you get if you use
Double.Parse(s);

Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]
Posted

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 ;).

Nothing is as illusive as 'the last bug'.
Posted

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...

Posted

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.

Nothing is as illusive as 'the last bug'.
Posted

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 -

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...