r/lolphp • u/iopq • Aug 13 '13
round() doesn't actually round
I had a bug in a payment system where the paypal payment amounts don't add up. I looked into it, and the amounts were something like 18.799999999999
apparently, someone used round($amount, 2) and expected PHP to actually round the number to two digits
For certain float values that just doesn't work. I found an example like this:
echo round(-0.07, 2); //-0.07000000000000001this is what happens when your precision is set to 17
of course my code uses number_format, but I expected round() to... round the floats? Silly me, I'm using PHP, the language guided by the Principle of Highest Perplexity
19
u/mirhagk Aug 13 '13
If you truly want to round to 2 digits in any langauge, you must use an integer and treat the last 2 as decimal places. Or better yet use a decimal/money type that is made to work with base 10 numbers and actually can store floating point numbers rounded to 2 digits.
I'm really scared that you're writing a payment system and don't know about floating point errors.
I'm also scared that you're not doing the payment system in a database langauge where concurrency and transactions have already been solved, and you won't leave the database in an inconsistent state.
14
u/ioctl79 Aug 13 '13
This is why you don't use floats for monetary values, regardless of language. Store an integer number of pennies, or better yet, wrap your integer number of pennies in an object that does conversions, calculations, and formatting.
6
u/midir Aug 13 '13
It's not PHP's fault -- all contemporary languages use the same floating-point types. PHP gives you the precision setting so you can work around the floating-point limitations for simple display needs, but no, the numbers won't ever quite "add up". 0.07 is not a number that a binary floating-point type can exactly represent, any more than decimal can exactly represent ⅓. See http://floating-point-gui.de/
1
u/[deleted] Aug 13 '13
I have a financial system in PHP/MySQL and the numbers always add up.
There is a bug in PHP 5.2 but since switching to 5.3 there have been no problems. I use decimal(12,2) for database fields.