Take the 2-minute tour ×

I have an ojbect $order that has a member function amount() that adds up the sum of the line items (all floats) then returns the value (again, as a float).

    $order->amount() * 100.0, 
    intval($order->amount() * 100.0),


    [0] => 7500
    [1] => 7499

The values of the line items are all under 1000 and never have more than 2 decimal points...

What is the best way to deal with decimal values in PHP, since the lack of precision is causing problems for me...

EDIT: To better illustrate:

echo "Addition: " . intval((172.45 - 72.45 - 25.0) * 100);


Addition: 7499
share|improve this question
As stated above, everything is a float, no strings, no ints (except for that intval() ) –  Ian May 1 '12 at 2:54
This is why we don't use floats in finance. –  Ignacio Vazquez-Abrams May 1 '12 at 3:01
There seems to be plenty of discussion on this topic if you search around. stackoverflow.com/questions/4662138/… OR stackoverflow.com/questions/1081988/… –  StressedEric May 1 '12 at 3:04
What's the alternative in PHP? There is no decimal type in PHP that keeps its precision... –  Ian May 1 '12 at 3:04
@ian: do everything in cents instead of dollars. That eliminates having to drag around 2 decimal places. –  Marc B May 1 '12 at 4:00

Your Answer



Browse other questions tagged or ask your own question.