# Help calculating with a STRING variable

2 replies to this topic

### #1 rodnkorpse

rodnkorpse
• New Members
• Newbie
• 2 posts

Posted 01 June 2006 - 01:12 AM

Can someone help me with this code.
I am working with a PHP textbook and an example isn't working correctly.
It is adding up totals for an order (from an order form that is keyed in by user) and showing total with tax.
My problem is whenever the total is over 999.99 the tax formula doesn't work.
I have figured out that is because it \$totalamount variable is a "string" so \$1,000 plugged into the formula it only sees the numbers UP TO the comma. So it calculates 1 * 0.1 instead of 1000 * 0.1

Any help on how to get past this problem?

<?
echo "<p>Your order is as follows:";
echo "<br>";
echo \$tireqty." Tires<br>";
echo \$oilqty." Bottles of Oil<br>";
echo \$sparkqty." Spark Plugs<br>";
define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPRICE", 4);
\$totalqty = \$tireqty + \$oilqty + \$sparkqty;
\$totalamount = \$tireqty * TIREPRICE + \$oilqty * OILPRICE + \$sparkqty * SPARKPRICE;
\$totalamount = number_format(\$totalamount, 2);
echo "<br>";
echo "Items Ordered: ".\$totalqty."<br>";
echo "Subtotal: \$".\$totalamount."<br>";
echo gettype(\$totalamount)."<br>"; //I added this in myself just to see the variable type
echo \$totalamount."<br>";
\$taxrate = 0.10;
\$tax = \$totalamount * \$taxrate;
\$totalamount = \$totalamount + \$tax;
echo "Tax amount: ".\$tax."<br>";
\$totalamount = number_format(\$totalamount, 2);
echo "Total including tax: \$".\$totalamount."<br>";

?>

### #2 litebearer

litebearer
• Members
• 2,357 posts
• Locationwhite lake michigan

Posted 01 June 2006 - 01:26 AM

```\$totalamount = \$tireqty * TIREPRICE + \$oilqty * OILPRICE + \$sparkqty * SPARKPRICE;
\$totalamount = number_format(\$totalamount, 2);
echo "<br>";
echo "Items Ordered: ".\$totalqty."<br>";
echo "Subtotal: \$".\$totalamount."<br>";
echo gettype(\$totalamount)."<br>"; //I added this in myself just to see the variable type
echo \$totalamount."<br>";
\$taxrate = 0.10;
\$tax = \$totalamount * \$taxrate;
\$totalamount = \$totalamount + \$tax;
echo "Tax amount: ".\$tax."<br>";
\$totalamount = number_format(\$totalamount, 2);
echo "Total including tax: \$".\$totalamount."<br>";
```

we do....
```\$sub_totalamount = \$tireqty * TIREPRICE + \$oilqty * OILPRICE + \$sparkqty * SPARKPRICE;
\$taxrate = 0.10;
\$tax = \$sub_totalamount * \$taxrate;
\$totalamount = \$sub_totalamount + \$tax;
echo "Sub total before taxes..... " .  number_format(sub_\$totalamount, 2) . "<br>";
echo "taxes............................. " .  number_format(\$tax, 2) . "<br>";
echo "Grand Total ................... " . number_format(\$totalamount, 2) . "<br>";```

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

### #3 rodnkorpse

rodnkorpse
• New Members
• Newbie
• 2 posts

Posted 01 June 2006 - 03:59 PM

So the only difference you did was do the math calculation BEFORE passing it thru the number_format().
Why does \$totalamount = number_format( \$totalamount, 2) make it become a string??

I can't believe an error as simple as that was included in sample code in a textbook. You'd think they'd know what they're writing about!

Thanks for the help.

R.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users