Raphael
Raphael

Reputation: 171

C# - Unexpected results when converting a string to double

I have a string with value "20.616378139" and when i try to convert using Convert.ToDouble or Double.Parse i get 20616378139.0 insted of the right value.

Why is this happening and how should I fix it?

Upvotes: 3

Views: 224

Answers (3)

masoudvaio
masoudvaio

Reputation: 1

I've used this command and there is no problem for me before.

string s = "20.616378139"; double d = Convert.ToDouble(s); ![enter image description here][1]

Upvotes: -2

Parmenion
Parmenion

Reputation: 299

There's an overload to the Parse method that provides an options parameter of some kind; this is the way that you can specify for it to handle scientific notation, etc. Try setting that explicitly. If that works, then look at the default culture info settings you are using.

Upvotes: -2

Hans Passant
Hans Passant

Reputation: 941465

You probably live in a part of the world where the decimal point is written as a comma. Fix:

var str = "20.616378139";
var dbl = double.Parse(str, System.Globalization.CultureInfo.InvariantCulture);

Upvotes: 16

Related Questions