cloudbust Posted February 27, 2006 Share Posted February 27, 2006 Greetings,Please have patience with this newbie.I am trying to learn PHP and have run into a seemingly bizarre problem.I am trying to execute a simple tax calculation and am getting inconsistent results.The server is running PHP 4.4.1 with Zend Engine v1.3.0 on a Linux (2.4.27?) server with Apache 1.3.33.You can access my test page here: Bob's Bits. If subtotal is less than $1000.00, the 10% tax is correctly calculated. Any subtotal above that does not calculate correctly. Try with 9 tires then with 10 tires as values. I have tried many ways of restructuring the statements to no avail. I have tried using 10 for $taxrate then dividing by 100 in the tax calculation. I have tried removing the decimal completely, but the calculation still performs the same way (Subtotal $900 gets $9000.00 tax, subtotal $1000 gets $10.00 tax). I have tried removing the number_format function. The portion of code that seems affected is: $taxrate=.10;echo 'Subtotal: $'.number_format($subtotal,2).'<br>';echo 'Tax Rate: '.($taxrate*100).'%<br>';echo 'Tax: $'.number_format($subtotal*$taxrate,2).'<br>';echo 'Total: $'.number_format($subtotal*(1+$taxrate),2).'<br>'; The subtotal always calculates correctly.I did create a separate php page with a hard coded subtotal amount in the calculation that works properly. that can be seen here:Calc Test.The code for that is: $subtotal=1100;$taxrate=.10;echo 'Subtotal: $'.number_format($subtotal,2).'<br>';echo 'Tax Rate: '.($taxrate*100).'%<br>';echo 'Tax: $'.number_format($subtotal*$taxrate,2).'<br>';echo 'Total: $'.number_format($subtotal*(1+$taxrate),2).'<br>'; I appreciate any assistance with this.Just in case it is being caused by something outside that code block and you are actually still reading this, here is the full code: <html> <head> </head> <title> Bob's Bits - Order Results </title> <body> <h1> Bob's Bits </h1> <h2> Order Results </h2> <?php // Create and load short variable names $tireqty = $_POST['tireqty']; $oilqty = $_POST['oilqty']; $sparkqty = $_POST['sparkqty']; // Calculate total items ordered - this will also be used to validate entry $totalqty = 0; $totalqty = $tireqty + $oilqty + $sparkqty; // Create and assign constants for pricing DEFINE ('tireprice', 100.00); DEFINE ('oilprice', 2.00); DEFINE ('sparkprice', 1.00); // Temporarily displays status of variable fields using isset /* echo 'isset($tireqty): '.isset($tireqty).'<br />'; echo 'isset($oilqty): '.isset($oilqty).'<br />'; echo 'isset($sparkqty): '.isset($sparkqty).'<br />'; echo 'empty($tireqty): '.empty($tireqty).'<br />'; echo 'empty($oilqty): '.empty($oilqty).'<br />'; echo 'empty($sparkqty): '.empty($sparkqty).'<br />'; echo 'Variable totalqty= '.$totalqty.'<br />'; */ // Display results if( $totalqty == 0 ) // No items were ordered so display an error message { echo '<font color=red />'; echo 'You did not order anything on the previous page! <br />'; echo '</font>'; } else // At least one item was ordered so display order details { echo '<p>Order Processed at '.date('H:i').' on '.date(' F jS Y').'</p>'; if ($tireqty > 0) { /* Incorporate a volume discount algorithm for tires such that: 1 - 10 tires = 0% discount 10 - 49 tires = 5% discount 50 - 99 tires = 10% discount 100 or more tires = 15% discount */ if ($tireqty < 10) $discount = 0; elseif ($tireqty > 9 && $tireqty < 50) $discount = 0; // should be 5 elseif ($tireqty > 49 && $tireqty < 100) $discount = 10; elseif ($tireqty > 99) $discount = 15; echo 'Tires: '.$tireqty.'@ $'.number_format(tireprice,2).' each: $'.number_format($tiretotal=($tireqty*(tireprice -(tireprice*$discount/100))),2).'<br>'; } if ($oilqty > 0) echo 'Oil: '.$oilqty.'@ $'.number_format(oilprice,2).' each: $'.number_format($oiltotal=($oilqty*oilprice),2).'<br>'; if ($sparkqty > 0) echo 'Spark Plugs : '.$sparkqty.'@ $'.number_format(sparkprice,2).' each: $'.number_format($sparktotal=($sparkqty*sparkprice),2).'<br>'; echo 'Subtotal: $'.$subtotal=number_format(($tiretotal+$oiltotal+$sparktotal),2).'<br>'; $taxrate=.10; echo 'Tax: $'.number_format($subtotal*$taxrate,2).'<br>'; echo 'Total: $'.number_format($subtotal*(1+$taxrate),2).'<br>'; echo 'Total items: '.($tireqty + $oilqty + $sparkqty); } ?> <p>Page 47</p> </body></html> Link to comment Share on other sites More sharing options...
gsmith Posted February 27, 2006 Share Posted February 27, 2006 My best guess would be to try defining your $subtotal variable outside of the echo statement. Then use $subtotal in the echo statement instead of $subtotal=..... Link to comment Share on other sites More sharing options...
sbrownii Posted February 27, 2006 Share Posted February 27, 2006 like gsmith said echo 'Subtotal: $'.$subtotal=number_format(($tiretotal+$oiltotal+$sparktotal),2).'<br>'; Is your problem.It is assigning the subtotal as a formatted string to $subtotal.Until you get 10 tires the subtotal doesn't go over 999 so there is no comma until then.Try the following code to get a better idea what is going on.... echo ("1,000.00" * 1); What does it print????? not "1000" as you might hope...If you want to stay with the one-liner (for the sake of keeping yourself and everyone else confused about what is going on!) try this echo 'Subtotal: $'.number_format($subtotal=($tiretotal+$oiltotal+$sparktotal),2).'<br>'; The above is setting $subtotal to the actual sum - not the formatted string! So even if you do the assignment in a separate line, make sure you are assigning the sum of the numbers to $substring - don't use nubmer_format() until the echo! Link to comment Share on other sites More sharing options...
cloudbust Posted February 28, 2006 Author Share Posted February 28, 2006 My best guess would be to try defining your $subtotal variable outside of the echo statement. Then use $subtotal in the echo statement instead of $subtotal=.....<{POST_SNAPBACK}> That did indeed fix the problem. Thank you for your assistance! Link to comment Share on other sites More sharing options...
cloudbust Posted February 28, 2006 Author Share Posted February 28, 2006 like gsmith saidecho 'Subtotal: $'.$subtotal=number_format(($tiretotal+$oiltotal+$sparktotal),2).'<br>'; Is your problem.It is assigning the subtotal as a formatted string to $subtotal.Until you get 10 tires the subtotal doesn't go over 999 so there is no comma until then.Try the following code to get a better idea what is going on.... echo ("1,000.00" * 1); What does it print????? not "1000" as you might hope...If you want to stay with the one-liner (for the sake of keeping yourself and everyone else confused about what is going on!) try this echo 'Subtotal: $'.number_format($subtotal=($tiretotal+$oiltotal+$sparktotal),2).'<br>'; The above is setting $subtotal to the actual sum - not the formatted string! So even if you do the assignment in a separate line, make sure you are assigning the sum of the numbers to $substring - don't use nubmer_format() until the echo! <{POST_SNAPBACK}> While the reply by gsmith gave me an immediate remedy, yours helped me realize why it was happening so I thank you also. I had not realized that the function was returning a string vs. a float. DOH!! - READ THE DOCS - I GET THAT NOW!!! That is what happens when one is too broke to pay attention.Thanks again! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now