We have a problem with rounding floating point numbers for some financial calculations.
Basically, we want to round monetary amounts, for example, from 1,000,000,000,555 to 2 decimal places. However, the float representation of this number is 1,000,000,000.554,9999, and as a result, we round up to 1,000,000,000.55 instead of the correct 1,000,000,000.56.
Is there any way to solve this problem in a safe way? The only thing that we have come up with so far is to always add the smallest represented float to the amount before the rounding operation, but we do not know if this is safe in all cases.
The code is written in C and should run on windows32 / 64 / linux / solaris, so we, unfortunately, do not have access to good materials, such as the decimal data type in .net.
Any input would be helpful.
Thanks Ricard
Rickard
source
share