Strange behavior of Double.ToString ()

I am trying to convert a double value 9007199254740992.0to a string.

But there seems to be a rounding error (last 2 become 0):

(9007199254740992.0).ToString("#")    // Returns "9007199254740990"
(9007199254740992.0).ToString()       // Returns"9.00719925474099E+15"

At first I thought that maybe the number could not be represented as double. But it is possible. This can be seen by dropping it to the end, and then converting it to a string.

((long)9007199254740991.0).ToString() // Returns "9007199254740991"
((long)9007199254740992.0).ToString() // Returns "9007199254740992"

In addition, I found that if I use the "R" format , it works.

(9007199254740992.0).ToString("R")    // Returns "9007199254740992"

Can someone explain why it ToString("#")doesn't return double with integer part with full precision?

+4
source share
1 answer

As seen on MSDN :

15 17 . 15 , ToString PositiveInfinitySymbol NegativeInfinitySymbol . , "G17", 17 "R", 15 , 17 , .

+5

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


All Articles