posted 19 years ago
Hi,
We are converting a monetary value (say dollars and cents) which is stored in a Double into an integer which stores the number in cents by multiplying the value by 100. I would have expected this to work without a problem but I recently discovered that we do not always get the value we expected e.g. if a double contains the value 2.07 and is multiplied by 100 the result is not 207.000 but 206.99999999999997
But if 2.18 is multiplied by 100 the result is 218.00000000000003.
This can be seen by executing the following code
Im sure that this is some feature of the IEEE floating point standard that I dont fully understand but what now concerns me is how I can fix this problem so that the cent value is always correct. Would it be enough to round my double value up before converting it to an int as follows
All comments appreciated.
John