Jump to content

Archived

This topic is now archived and is closed to further replies.

rodnkorpse

Help calculating with a STRING variable

Recommended Posts

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>";

?>

Share this post


Link to post
Share on other sites
How about instead of...
[code]
$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>";
[/code]

we do....
[code]
$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>";[/code]

Lite...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.