Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 376
João Matos Avatar asked Apr 05 '09 18:04

João Matos


People also ask

Can you cast a string to a double?

We can convert String to double in java using Double. parseDouble() method.

Can you turn a string into a double C++?

In C++, the stod() function performs a string to double conversion.

How do you convert a string to a double in Python?

Use float() method or decimal() method to convert string to double in Python. Conversion of string to double is the same as the conversion of string to float.

How do I convert a string to a double in C #?

String value can be converted to double using Convert. ToDouble() or Double. Parse() method. These methods take string representation of a number as input and return its equivalent double-precision floating-point number.


1 Answers

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);
like image 66
mmx Avatar answered Sep 25 '22 16:09

mmx