Converting double to integer function fails with some operations - page 2

 
William Roeder:

Up to 15 significant digits.
          Double-precision floating-point format - Wikipedia, the free encyclopedia

Thank you for this information.


Then (if I understand it well) we can use (100000 have less than 15 significant digits):

int Value= int(round(PassedPrice*100000)+0.1);

but we cannot use:

int Value= int(round(PassedPrice/_Point)+0.1);

because in this example _Point=0.00001 but this number could be represented by less or greater number than 0.00001 (e.g. 0.0000099999...) then we could get wrong result.

Of course, this also applies to your example.


If we want to be precise and don't use hard-coding numbers we have to use something like this:

int Value= int(PassedPrice*int(1/_Point+0.5)+0.5);
 

For 5 digits symbols is _Point=0.00001 but it is represented by a little bit greater number. Then if you divide by _Point you don't get 100000 but 99999.99999... Then you have to add something (0.1 is enough) before you convert it to an integer.

UPDATE:

This also applies to 0, 1, 2, 3, 4 digits symbols. Then the general solution (IMHO) is:

int Value= int(PassedPrice*int(1/_Point+0.1)+0.5);