Click to See Complete Forum and Search --> : cannot display right digit in php
cyh781s
02-06-2003, 02:04 AM
Hi, recently i made a simply order form using php.
the php page will display quantity, subtotal, and total-after-tax.
the quantity and subtotal work perfect no matter how large number i input.
Now here is the problem,
if the subtotal is not over $1000, the total-after-tax works fine. (assume tax rate is 10%)
for example, subtotal is $940, the total-after-tax will be $1034.00.
however, when the subtotal is over $1000, the total-after-tax will show me 2 integer + 2 decimal only.
for example, subtotal is $1040, the total-after-tax gets me $1.10 instead of $1144.00
Can anyone tell me how to solve this problem? maybe i need to setup somehting in somewhere.
Thanks in advance
Choozo
02-07-2003, 11:00 AM
Do you store your integers in a database (MySQL)?
If so, check the datatype assigned to those integers (short int, long int, etc.), as you may have a 'overflow' problem when your integer value exceeds the space set aside for it.
E.g. trying to store a 6-bit DIGITAL value "100010" in a 4-bit datatype may be seen like this "10 0010" (two positions chopped off giving you a significantly smaller number).
Just a hunch ...
cyh781s
02-07-2003, 02:11 PM
I haven't used mySQL to store my data yet.
All i did was to echo the result on the php page. This was why I felt weird:confused: .
As the example I gave above, it looked like I could echo quantity * price, which was all integer,
but could not echo quantity * price * ( 1 + taxrate) , which was a double amount, to a right digit amount.
the php book and web i read all told me that I do not need to declare variable types. what i assign to it is what it is.
Thanks again.
:)
HighOrbit
02-07-2003, 03:14 PM
post your script and we can take a look at it
cyh781s
02-07-2003, 03:36 PM
here is the php part:
<?php
echo "Order processed at ";
echo date("H:i, jS F");
$date = date("H:i, jS F");
echo "<br>";
echo "<p>Your order is as follows:";
echo "<br>";
$totalqty = $_POST["tireqty"]+$_POST["oilqty"]+$_POST["sparkqty"];
echo "<br>";
define("TIREPRICE",100);
define("OILPRICE", 10);
define("SPARKPRICE", 4);
$totalamount = $_POST["tireqty"] * TIREPRICE +
$_POST["oilqty"] * OILPRICE +
$_POST["sparkqty"] * SPARKPRICE;
$totalamount = number_format($totalamount, 2);
echo "Items orderd: ".$totalqty."<br>\n";
echo "Subtotal: $".$totalamount."<br>\n";
$taxrate = 0.10;
$totalamount =$totalamount * (1+$taxrate);
$totalamount = number_format($totalamount, 2);
echo "Total including tax: $".$totalamount."<br>\n";<---- problem showing right digit
echo "<br>\n";
nouse66
02-07-2003, 03:46 PM
well why are you doing:
$".$totalamount."
instead of just \$$totalamount ?
maybe i'm wrong but that's how i'd do it.
cyh781s
02-07-2003, 03:58 PM
$totalamount is where I assign the number to it.
I tried your code, it compiled ok but showed me no result.
i change my code: echo "Total including tax: $".$totalamount."<br>\n";
to
your recommnadation: echo "Total including tax: ".\$$totalamount."<br>\n";
and here is what the php page showed:
Total including tax: <blank>
maybe i didn't open some rules in my php so this code seems not work on my php. and this is the reason i need to use $_POST["tireqty"] instead of just $tireqty.
still thank you for your help :)
nouse66
02-07-2003, 04:12 PM
i understand what you're doing now. you dont need to keep the variable outside of qoutes. echo will parse the statement for variables
i meant this:
echo "Total including tax: \$$totalamount<br>";
if you want to do it your way you might want to keeps spaces around the '.' for concatination. i think i read somewhere that its "supposed to be that way"
echo "Total including tax: $" . $totalamount . "<br>\n";
cyh781s
02-07-2003, 04:22 PM
I tried.........but still got same result.
if subtotal exceed 1000, the total after tax will have 2 integer and 2 decimal.
I do learn some new way to code php though. :D
thanks for the help
Maybe I should just use database instead.
nouse66
02-07-2003, 04:39 PM
thats really weird. i'm stumped.
i tried testing a portion or your code with the command line php and it worked fine....
bash-2.05b$ cat test.php
<?php
$totalamount = 1099;
$taxrate = 0.10;
$totalamount =$totalamount * (1+$taxrate);
$totalamount = number_format($totalamount, 2);
echo "Total including tax: $".$totalamount."<br>\n";
echo "<br>\n";
?>
bash-2.05b$ php test.php
Total including tax: $1,208.90<br>
<br>
bash-2.05b$
Choozo
02-07-2003, 04:55 PM
Yes, this is indeed a odd and intriguing problem. Tried this myself in a php script, with the same bogus values above 1000.
// numbertest.php
<?php
define("TIREPRICE",100);
define("OILPRICE",10);
define("SPARKPRICE",4);
$date = date("H:i, jS F");
if ($post) {
echo "Order processed at $date";
echo "<p>Your order is as follows:<br>";
$totalqty = $_POST["tireqty"] +
$_POST["oilqty"] +
$_POST["sparkqty"];
$totalamount = $_POST["tireqty"] * TIREPRICE +
$_POST["oilqty"] * OILPRICE +
$_POST["sparkqty"] * SPARKPRICE;
$totalamount = number_format($totalamount,2);
$taxrate = 0.10;
echo "Items ordered: $totalqty<br>";
echo "Subtotal: $totalamount<br>";
$totalamount = $totalamount * (1 + $taxrate);
$totalamount = number_format($totalamount,2);
echo "Total including tax: $totalamount";
} else {
echo "<form method=\"post\" action=\"numbertest.php\">
Tires: <input type=\"text\" size=\"3\"name=\"tireqty\"><br>
Oil: <input type=\"text\" size=\"3\" name=\"oilqty\"><br>
Plugs: <input type=\"text\" size=\"3\" name=\"sparkqty\"><br>
<p><input type=\"submit\" value=\"Submit\">
<input type=\"hidden\" name=\"post\" value=\"1\">
<input type=\"reset\" value=\"Reset\">
</form>";
}
?>
Since it returned expected values when executed from commandline, I guess something gets screwed up in the apache module?
Maybe you could post this problem in the forum over at www.phpbuilder.com ?
There should be a few PHP experts there (along with the developers of PHP).
cyh781s
02-07-2003, 08:16 PM
nouse66, yes, if i just type in solid number directly to my php page, it works fine cuz i just try it. :)
so i guess the problem is like Choozo said it's apache server setting problem.
I must have set something wrong or what. I will try to look it up.
Thanks guys, you guys are great :)