Strange problem with number_format()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Strange problem with number_format()

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Post Reply