Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Convert String to double loss of precision

Tags:

c#

I have the following:

string value = "9223372036854775807";
double parsedVal = double.Parse(value, CultureInfo.InvariantCulture);

... and the result is 9.2233720368547758E+18 which is not the exact same number. How should I convert string to double without loss of precision?

like image 916
user1001371 Avatar asked Dec 13 '22 06:12

user1001371


1 Answers

double can only guarantee 16 (approx) decimal digits of accuracy. You might try switching to decimal (which has a few more bits to play with, and holds that value with plenty of headroom).

like image 64
Marc Gravell Avatar answered Feb 08 '23 23:02

Marc Gravell