Problems with floats dropping £1000
Posted: Fri Oct 29, 2010 4:57 am
Hi, I'm new to php and am usually a violinist! I have written a shopping cart for a friends site, and populate $_SESSION with the $total_price. It works fine most of the time, but when an order was placed for £1,094.16, and the script added the postage (£31.37), the £1,094.16 was devalued to £1.00 (luckily it's not live yet!!!).
The price is displayed fine before postage is added, suggesting it is stored corectly in the $_SESSION variable. It goes adrift in the calculation:
$grand_total = (number_format(($_SESSION['total-price']), 2)) + (number_format($post_cost, 2));
$grand_total = number_format($grand_total, 2);
Any help with this would be appreciated. As the language is dynamically typed, I'm assuming it's not a casting issue.
Cheers,
Phil
The price is displayed fine before postage is added, suggesting it is stored corectly in the $_SESSION variable. It goes adrift in the calculation:
$grand_total = (number_format(($_SESSION['total-price']), 2)) + (number_format($post_cost, 2));
$grand_total = number_format($grand_total, 2);
Any help with this would be appreciated. As the language is dynamically typed, I'm assuming it's not a casting issue.
Cheers,
Phil