[interchange-bugs] [rt.icdevgroup.org #349] Vend::Util::round_to_frac_digits() simplification
Peter Ajamian via RT
interchange at rt.icdevgroup.org
Wed Mar 21 21:33:24 UTC 2012
<URL: http://rt.icdevgroup.org/Ticket/Display.html?id=349 >
On 22/03/12 09:34, Mark Overmeer via RT wrote:
> It is not a rounding error in sprintf here, but in converting 0.125 into a
> floating-point number. Probably it gets converted into 0.12499999999999
> When you round that, you get 0.12 as shown.
Right, that's what I meant.
> So, you indicate a common problem with floats. Two solutions:
> . always administer money as integer cents
Which is basically what we do, but then we have to round them with our
own function becaquse sprintf doesn't do rounding of integers, so we're
right where we are now.
> . add 0.001 to sprintf (which I forgot in my original bug-report)
> markov at moon> perl -le 'print sprintf "%.*f", 2, 0.125 +0.001'
> markov at moon> perl -le 'print sprintf "%.*f", 2, 0.1251 + 0.0001'
Right, I even tried this by multiplying by 1.0000000001 (or something
like that) to just nudge the value up a tiny bit and it worked (adding
will have the problem that it won't work correctly for negative
numbers). The issue here, though, is that floats are unreliable for
financial calculations and it's better to just avoid them than to try to
overcome their problems with workarounds like this.
So we're basically back to just fixing the current function for the
problems you pointed out originally.
More information about the interchange-bugs