RE: basic variable question

From: Date: Fri, 14 Feb 2003 06:41:03 +0000
Subject: RE: basic variable question
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-18535@lists.php.net to get a copy of this message
/********** Hi everyone. Why does my code work when I enter just 1 for the quantity, but only returns $1.10 when I enter a quantity of 2 or more? *********/ because number_format() returns strings and when you do $totalamount = number_format($totalamount, 2); you are converting $totalamount to string whose behaviour is uncertain in subsequent arithmetic operations. This works: <?php $totalqty = 0.00; $totalamount = 0.00; define("HALLTABLE", 800); $totalqty = $_GET["hallqty"]; //quantity $totalamount = $_GET["hallqty"] * HALLTABLE; //total cost // $totalamount = number_format($totalamount, 2); echo number_format($totalamount, 2); echo "<br>\n"; echo "Items ordered: ".$totalqty."<br>\n"; echo "Subtotal: $".$totalamount."<br>\n"; $taxrate = 0.10; // local sales tax is %10 $totalamount = $totalamount * (1 + $taxrate); $totalamount = number_format($totalamount, 2); echo "<br>\n"; echo "Total including tax: $".$totalamount."<br>\n"; ?> -----Original Message----- From: paradiddles [mailto:fojomo@sbcglobal.net] Sent: Friday, February 14, 2003 00:30 To: php-windows@lists.php.net Subject: basic variable question Hi everyone. Why does my code work when I enter just 1 for the quantity, but only returns $1.10 when I enter a quantity of 2 or more? $totalqty = 0.00; $totalamount = 0.00; define("HALLTABLE", 800); $totalqty = $_POST["hallqty"]; //quantity $totalamount = $_POST["hallqty"] * HALLTABLE; //total cost $totalamount = number_format($totalamount, 2); echo $totalamount; echo "<br>\n"; echo "Items ordered: ".$totalqty."<br>\n"; echo "Subtotal: $".$totalamount."<br>\n"; $taxrate = 0.10; // local sales tax is %10 $totalamount = $totalamount * (1 + $taxrate); $totalamount = number_format($totalamount, 2); echo "<br>\n"; echo "Total including tax: $".$totalamount."<br>\n"; ---------------- "forget your lust for the rich man's gold. All that you need, is in your soul. You can do this if you try. All that I want for you my son, is to be satisfied" ~ Lynard Skynard

« previous php.windows (#18535) next »