João Matos
João Matos

Reputation: 1545

Converting a string to a double

I'm trying to convert a string to a double value but it's not returning me what I expect...

double dbl;
Double.TryParse("20.0", out dbl);

That piece of code is returning 200.0 (instead of 20.0) as a double value. Any idea why?

Upvotes: 5

Views: 925

Answers (1)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422132

You should pass InvariantCulture to the method.

The reason behind this is that your regional settings probably set . as separator character and not decimal point.

double.TryParse("20.0", NumberStyles.Any, 
                CultureInfo.InvariantCulture, out x);

Upvotes: 22

Related Questions