Double conversion problem

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

Hi there,

I'm having a weird situation when converting strings to double.
Normally, the following code would work right?

double MyDouble = double.Parse("20.50"); // Error at runtime

Well, it gives me an error at run time, telling me that the format of
the input string is incorrect. Then, if i try with a comma instead of
a dot, it works fine.

double MyDouble = double.Parse("20,50"); // Works fine

Isn't it supposed to be a dot that works? I think it could be related
to "Regional and Language Options" in WinXP. I'm using french canadian
regional options if it can gives you a hint. And if it is really
related to regional and language options, what should i change
exactly?

If anyone has an idea how to make it works with a dot that would be
great. I want understand why it doesn't works. Thanks!
 
I'm sure it is your regional settings.

You will notice that in the Regional Options Tab (in the Regional and
Language Options) the samples will show you how the number is to be
formatted. For French Canadian, a number is this:

"123 456 789,00"

which would explain your error. Change the format to English and you won't
have the problem.

If you must have the french Canadian, then you have to use that number
format, where a comma is the decimal separator. You could also customize
and change the decimal to a period.

Hope this helps.
 
double MyDouble = double.Parse("20.50"); // Error at runtime

Well, it gives me an error at run time, telling me that the format of
the input string is incorrect. Then, if i try with a comma instead of
a dot, it works fine.

double MyDouble = double.Parse("20,50"); // Works fine

Isn't it supposed to be a dot that works? I think it could be related
to "Regional and Language Options" in WinXP. I'm using french canadian
regional options if it can gives you a hint. And if it is really
related to regional and language options, what should i change
exactly?

If anyone has an idea how to make it works with a dot that would be
great. I want understand why it doesn't works. Thanks!

Try:
double.Parse("20.50", new System.Globalization.CultureInfo("en-US"));

And yes, it's about regional options... :)
 
Well it was the regional settings. I just set it to Canadian English
and it works fine for both.

However it means that this section of code will depend of the user's
regional settings. Fortunately for me it's an asp.net project that
will run on a specific server. But if it was a c# program made for
deployment on different computers, that would be problematic.
 
Matt,

Everyone has this number from string parse problem. As an American
living in Europe, with a french computer programing for the US, I got
to it quicker. If you need to reliably convert a number to and from a
string, there is nothing better than "forcing" the culture to
CultureInfo.InvariantCulture. You will find it (if not exactly) very
similar to en-US. so parsing would go like:
double dbl = double.Parse("1234.56",
System.Globalization.CultureInfo.InvariantCulture);

And when you need to get your string from the double, you can do the
same thing in "reverse":
string s =
dbl.ToString(System.Globalization.CultureInfo.InvariantCulture);

Scott
 
Back
Top