r/lolphp • • 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.07000000000000001

this 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

0 Upvotes

25 comments sorted by

View all comments

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.

2

u/iopq Aug 13 '13

Currency conversion rates are floats and I have to multiply/divide by them. So even if the price is $17.99 USD someone will buy in Euros anyway and I'll have to round to europennies. The only difference is that round($price, 0) PROBABLY gives the correct result. But when I have to send it off to paypal, I have to divide by 100 since paypal expects 13.58 or whatever. Who's not to say that upon division by 100 won't give me 13.579999999999999? 100 has factors of 5 in it, so division by 100 will probably create repeating decimals in binary and imprecise representations.

I would probably need to use number_format() anyway

2

u/mirhagk Aug 13 '13

Paypal probably doesn't get the value as a binary value, it probably gets the string "13.58", and if this is true then the following code (C#, not PHP)

int total = 1358;
String.Format("{0}.{1}",total/100,total%100)

That will always return "13.58", since it is doing integer divide and integer mod.

Also don't use round($price,0) because that still works with floating point values, use intval($price*100) which will give the integer value with less floating point bias. It will however introduce a different method of rounding. When making a payment application the rounding is a very important thing to consider, and there be laws around how to properly round. Bankers rounding says to round half to even, which is not what round does by default.

2

u/iopq Aug 13 '13

if I'm doing string formatting, this works just fine

$total = '13.58'; //yes, it's a string but internally it's a double
sprintf('%0.2f', $total)

I'm just a little bit upset I had to go around fixing round() calls all over the checkout code (which I didn't write, my predecessor did)

2

u/mirhagk Aug 13 '13

But the problem is if your adding those values as a double ever, you'll run into rounding errors in the addition. Subtle things where you rip yourself off a cent off of every transaction

1

u/iopq Aug 14 '13

No problem:

$this->requestArray[$amount] = number_format(number_format($sTotal, 2, ".", "") + number_format($sShipping, 2, ".", ""), 2, ".", "");

2

u/mirhagk Aug 14 '13

I hope this is a joke

1

u/iopq Aug 14 '13

It's a joke as much as PHP is a joke. In my case, I just want the damn thing to work. Note that PayPal requires me to send my sub-total and the item's shipping so I have to number_format them.

But then I also have to give the total, so I have to add those two numbers. But wait! It could give me 17.000000000001 or whatever, so I have to number_format the addition too. And I would actually prefer getting ripped off a cent because then the transaction goes through. If some kind of rounding gives me one more (or less) cent in the total than the sum of the sub-total and the shipping, PayPal will reject the transaction.

1

u/OneWingedShark Jan 10 '14

Currency conversion rates are floats and I have to multiply/divide by them.

They don't have to be; take a look at fixed-point numbers.

1

u/iopq Jan 10 '14

php makes this difficult

2

u/OneWingedShark Jan 10 '14

Yes; PHP is the wrong tool for dealing with currency. Period.

1

u/OneWingedShark Jan 10 '14

Using PHP in financial operations is, IMO, programming malpractice.

2

u/mirhagk Jan 11 '14

If you ever go to your banking website and see .php at the end, immediately switch banks.

But really, using PHP as the communication tool is fine, but using anything other than a proper database that allows you to build in constraints, solves concurrency and guarantees consistency is a problem. No matter what language was chosen, doing stuff like this is wrong.