C # Converting String to double precision loss

I have the following:

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

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

+4
source share
3 answers

You cannot convert 9223372036854775807 to double without loss of precision due to the definition of double (( IEEE 754 ) for binary floating-point arithmetic).

By default, a double value contains 15 decimal digits of precision, although a maximum of 17 digits is supported domestically.

+4
source

double can only guarantee 16 (approximate) decimal digits of precision. You can try switching to decimal (which has a few more bits for the game, and holds this value with a large margin).

+8
source

Using Decimal, you get the exact data you need. However, note that Decimal will not have the same storage size. like a double.

See Can C # store more accurate data than paired ones?

+1
source

Source: https://habr.com/ru/post/1391235/


All Articles