开发者

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?


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


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.


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]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜