• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Why is this not adding like I think it should..??

I've got the following within a script:

$ItemTotal = number_format($_POST['ItemTotal'],2);
$SalesTax = round($ItemTotal * ($SalesTaxRate / 100),2);
$HandlingTotal = number_format($_POST['HandlingTotal'],2);
$OrderTotal = round($ItemTotal + $SalesTax + $HandlingTotal, 2);

Then in my form I'm putting 1240.00 for $ItemTotal, $SalesTax and $HandlingTotal are both 0.00 right now.  $OrderTotal windes up giving me just 1 instead of $1,240.00 like I'm expecting.  What am I missing here..??
Andrew Angell
Andrew Angell
2 Solutions
number_format(1240.00, 2) returns the string "1,240.00"
Later, when you use $ItemTotal again, it treats $ItemTotal as its integer equivalent because of the addition operator. The integer equivalent of '1,240.00' is 1. Just don't use number_format there. Use it on $OrderTotal.
When you use number_format, the result is a string, instead of a number. This means that you can't do calculations with it. So what you should do is make the formatting the last step, after calculating all the numbers.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now