Jump to content

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

?>
Link to comment
https://forums.phpfreaks.com/topic/10910-help-calculating-with-a-string-variable/
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...
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.