Re: Need help

From: Date: Fri, 09 Apr 2004 16:35:29 +0000
Subject: Re: Need help
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-183011@lists.php.net to get a copy of this message
Strogg wrote:
This the script that is falling over..... It calculates everything correct until the amount reaches £1,000, and then it falls over and reads only the 1 before the comma, and then outputs the price to be paid as £1.18 (£1 + the taxrate (0.175 rounded up)) <? echo "<p>Order Processed at "; echo date("H:i, jS F"); echo "<br>"; echo "<p>You ordered:"; echo "<br>"; echo $tyreqty." Tyres<br>"; echo $oilqty." Bottles of Oil<br>"; echo $sparkqty." Spark Plugs"; $totalqty = 0; $totalamount = 0.00; define("TYREPRICE", 100); define("OILPRICE", 10); define("SPARKPRICE", 5); $totalqty = $tyreqty + $oilqty + $sparkqty; $totalamount = $tyreqty * TYREPRICE
                 + $oilqty * OILPRICE
     + $sparkqty * SPARKPRICE;
$totalamount = number_format($totalamount, 2); echo "<br>\n";
  echo "Items Ordered:     ".$totalqty."<br>\n";
  echo "Subtotal:          £".$totalamount."<br>\n";
$taxrate = 0.175; //local tax rate in UK is 17.5% $totalamount = $totalamount * (1 + $taxrate); $totalamount = number_format($totalamount, 2); echo "Total Including Tax: £".$totalamount."<br>\n"; ?> --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.655 / Virus Database: 420 - Release Date: 08/04/2004
Here's your problem: $totalamount = number_format($totalamount, 2); That creates a string with a comma in it. When you try to read it as a number again, the comma screws it up. Wait to do the number_format until you output like this:
echo "Subtotal:          £".number_format($totalamount, 2)."<br>\n";
... echo "Total Including Tax: £".number_format($totalamount, 2)."<br>\n"; In other words, instead of assigning the number formatted string back to $totalamount, format it only on output. -- paperCrane <Justin Patrin>

« previous php.general (#183011) next »