this is something very strange...in a foreach loop, i'm counting the items' quantity ($itemQty) and price ($itemSubtot). then there's a final total of prices ($total) and qty ($pcs). funny thing is, it all works until $itemSubTot hits $1000. that by itself displays correctly, but it then resets $total to $1.00! say i have 5 items, each with a subtotal displayed as $999.00. $total will display $4995.00. but if just one of those item's subtotal goes to $1000, $total will go to $1.00 + $3996...which is $3997! if it goes to $2000, total shows $2.00 and so on. what on earth is going on? here's a snippet of my code that deals with this. foreach { ...some code removed... $itemQty= $_SESSION['cartSizes'][$uniq_ID][1] + $_SESSION['cartSizes'][$uniq_ID][2]]; $output[] = '<td >'.$itemQty.'</td>'; $output[] = '<td>$'.$price.'</td>'; $itemSubTot = number_format($price * ($_SESSION['cartSizes'][$uniq_ID][1] + $_SESSION['cartSizes'][$uniq_ID][2], 2) ; $output[] = '<td>$'.$itemSubTot.'</td>'; $total += $itemSubTot; $pcs += $itemQty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Total Pcs: '.$pcs.'</p>'; $output[] = '<p>Purchase Subtotal: $'.number_format($total, 2).'</p>'; PHP:
When you use number_format for numbers above 999, it will return: 1,000 and this is a string and not an integer value. So PHP will try to convert it to an integer and make it into 1. You need to remove number_format from $itemSubTot line: to: Use number_format only when you want to print the number.
i just realized something and updated the root post. $total actually does go over $1000, but only if none of the $itemSubTot values go over $1000 themselves. so if there's 10 $itemSubTot to loop through and they're all under $1000 individually but collectively add up to over $1000, $total will work. both $itemSubTot and $total are printed to the screen. and even if an $itemSubTot is over $1000 by itself, it will print correctly, but when you add it to $total, $total will not display correctly. you way works, i just got rid of the number_format where i declared the variable, and just added number_format to when i need to display it. thanks!!!!
PHP will not round something automatically (as far as I know). If you do want to round $itemSubTot just add number_format to the output line:
yup, just got it! thanks man! i was thinking that php would round it first, so me using the number_format after the rounding would be useless. but thank god it's not the case!