rodnkorpse Posted June 1, 2006 Share Posted June 1, 2006 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.1Any 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 typeecho $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>";?> Quote Link to comment https://forums.phpfreaks.com/topic/10910-help-calculating-with-a-string-variable/ Share on other sites More sharing options...
litebearer Posted June 1, 2006 Share Posted June 1, 2006 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 typeecho $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... Quote Link to comment https://forums.phpfreaks.com/topic/10910-help-calculating-with-a-string-variable/#findComment-40758 Share on other sites More sharing options...
rodnkorpse Posted June 1, 2006 Author Share Posted June 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10910-help-calculating-with-a-string-variable/#findComment-40947 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.