2 ms·
> I am writing a simple financial application in a language without a decimal type (Go), and my workaround for this is to multiply by 100 when storing currency.
by lutusp 14y ago
> I am writing a simple financial application in a language without a decimal type (Go), and my workaround for this is to multiply by 100 when storing currency. The thought process was that at least cents will be represented exactly.
Yes, that will work -- until you need to compute taxes, discounts, or any other quantity that involves amounts less than a penny during interim computations. Then you're back to square one.
> Things like currency exchange would still benefit from a proper decimal type though.
Yes, but binary floating-point doesn't really pose a problem if handled carefully. The classic solution is to store all amounts internally in double-precision floating-point and only display the rounded values -- never truncate the values to the nearest penny as student programmers tend to do.
In the system described above, that used in all major financial packages, amounts like 0.1 simply aren't an issue, and every number base has amounts it can't represent exactly.