Why is the result of RoundTo(87.285, -2) => 87.28

后端 未结 2 1438
别那么骄傲
别那么骄傲 2020-12-05 07:29

I expected that the result would be 87.29. I also tried SimpleRoundTo, but produces the same result.

In the help there is also a \"strange\" example: ms-help://embar

2条回答
  •  攒了一身酷
    2020-12-05 08:17

    87.285 is not exactly representable and the nearest double is slightly smaller.

    The classic reference on floating point is What Every Computer Scientist Should Know About Floating- Point Arithmetic.

    For currency based calculations, if indeed this is, you should use a base 10 number type rather than base 2 floating point. In Delphi that means Currency.

提交回复
热议问题