Page 1 of 1

Strange problem with number_format()

Posted: Sun Jul 31, 2005 8:04 pm
by SBro
I have the following piece of code for a shopping cart which grabs the total of all items in the cart.

Code: Select all

foreach ($contents as $content) $totals['total'] += $content['total_price'];
For some reason the value of $totals['total'] is not formatted to 2 decimal points. Even though the values in $content['total_price'] are already formatted in this form. Even if i change the code to

Code: Select all

foreach ($contents as $content) $totals['total'] += number_format($content['total_price'], 2);
It has no effect and the amount is still not formatted correctly, anyone have any ideas? thanks.

Posted: Sun Jul 31, 2005 8:11 pm
by feyd
do the number_format() call after the sum is found, not during. You'll create an error when you go over 1000 with remainders..