Money type
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Thu Jan 2 18:20:38 UTC 2020
On Thursday, 2 January 2020 at 17:59:40 UTC, H. S. Teoh wrote:
> AFAIK, the crux of the problem with using binary floating point
> types for money representation is that the former's base-2
> rounding behaviour does not 100% match the latter's expected
> base-10 rounding behaviour. This discrepancy is a big problem
> because in financial applications 100% identical behaviour with
> base-10 rounding is a non-negotiable requirement.
You can set the rounding mode. Round to even is the common
default I believe (bankers rounding). Although I think it is
common to just round up at 0.5 when doing manual accounting.
So you can get the same behaviour when storing in cents, but you
then need to do the downscaling as the last step. So you need to
take care to get correct results.
E.g. to get 3% of 123 cents: (3*123.0)/100
Although 25% is no problem: 0.25*123.0 (since 0.25 has an exact
representation in base 2 floating point)
So it is easy to make mistakes. Same argument for fixed point.
With base 10 representation that is no longer a problem.
Same with base 2 rational, as you do the rounding in the last
step.
More information about the Digitalmars-d
mailing list