Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Unexpected results when converting a string to double

Tags:

c#

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?

like image 237
Raphael Avatar asked Feb 02 '26 03:02

Raphael


1 Answers

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);
like image 178
Hans Passant Avatar answered Feb 04 '26 16:02

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!