# Rounding Errors with Currency

A user questions a rounding issue on perl.

According to Perl, 100.10 - 100.00 = 0.0999999999999943The short answer is all fractions cannot be exactly represented in binary. A good decimal example of this problem is comparing the fraction one third (1/3) to its decimal representation (0.3333333333).

and not ".10" as I would have imagined from my early school days.

Try it yourself in your favorite shell (this happens in v5.83): perl -e "print (100.10-100);"

Can someone with supreme wisdom please explain to me why perl comes to this result from simple math with a non-whole number. This is causing thousands of calculations in my app to be off by .01 - .02 - but when money is involved it needs to be 100% correct.

A better answer is given by BroswerUk.

For a simple explanation of why floating point doesn't give exact results, see Re: Re: Re: Bug? 1+1 != 2.

On some VBA apps I wrote, I dealt with this problem by converting my float to string and back to float.

-Chris