after excluding 1000 total amount Tax rate total amount changers in to two digit

my order.php result
[center]enjoy the meal check your order below
50: CHICKEN MASALA for RS:5000
50: EGG OMLET for RS:2500
50: FISHFRY for RS:4750
50: ICECREAM for RS:4000
total bill amount excluding tax RS 16,250.00
total bill amount including tax RS 16.32 <==problem is here why i get two digit place after excluding 1000 total [/center]

below is my HTML code

[code]

Hotel
food Quantity Cost
Chicken Masala 100 RS
Egg Omlet 50 RS
Fish Fry 95 RS
Ice Cream 80 RS
note: above rates are for per quantity
0.5 service charge included on total amount [/code]

[php]<?php
//getting food qty via post method
$chickenmasala = $_POST[‘chickenmasala’];
$eggomlet = $_POST[‘eggomlet’];
$fishfry = $_POST[‘fishfry’];
$icecream = $_POST[‘icecream’];
//calculating total quantity
$totalqty=$chickenmasala + $eggomlet + $fishfry + $icecream;
//giving rates each defend method
define(“CHICKENMASALA”, 100);
define(“EGGOMLET”, 50);
define(“FISHFRY”, 95);
define(“ICECREAM”,80);
//Total amount getting by multiplying each quantity
$totalamount=$chickenmasala * CHICKENMASALA + $eggomlet * EGGOMLET + $fishfry * FISHFRY + $icecream * ICECREAM ;
//each order cost for
$chickenprise = $chickenmasala * CHICKENMASALA ;
$eggomletprise = $eggomlet * EGGOMLET ;
$fishfryprise = $fishfry * FISHFRY ;
$icecreamprise = $icecream * ICECREAM ;
//total amount 2 decimal format
$totalamount = number_format($totalamount,2);
//if else if
if($totalqty == 0){
echo “you have not ordered any food”;
}else{
echo"enjoy the meal check your order below
";
if($chickenmasala > 0){
echo $chickenmasala. ": CHICKEN MASALA for RS:$chickenprise
";

}
if($eggomlet > 0){

echo $eggomlet. ": EGG OMLET for RS:$eggomletprise
";

}
if($fishfry > 0){

echo $fishfry. ": FISHFRY for RS:$fishfryprise
";

}
if($icecream > 0){

echo $icecream. ": ICECREAM for RS:$icecreamprise
";

}

echo "total bill amount excluding tax RS $totalamount <br />"; 

//tax rate 

$taxrate[1]=0.5;
$taxrate[2]=100;
$taxrate[3]=$totalamount;
$totalamounttax = $taxrate[1]*$taxrate[3]/$taxrate[2];
$totalamount=$totalamount+$totalamounttax;
echo "total bill amount including tax RS $totalamount ";
}

?>[/php]

Well, I do not understand why you reposted this 3 times total. And, I did not understand your asking about 1000 total?

But, your tax amount calculations seem mixed up. It oddly adds in a tax item of 100 and this does not make sense to me. In simple terms, tax is handled using an easy calculation. It is just total + total*taxrate. There is no complicated way to do this. It is simple. You used a tax rate of .5 which is 50%. Assuming that you mean 5%, it should be .05. Using your own code, it would be something like this:
[php]
//tax rate
$taxrate = .05;
$totalamounttax = $totalamount * $taxrate;
$totalamount = $totalamount + $totalamounttax;
echo "total bill amount including tax RS $totalamount ";
[/php]
Of course, you do not need 3 lines to do this, you could just use:
$totalmount = $totalamount + $totalamount * .05;

A few notes on this code and your other code… First, you should use variables that make sense. Especially if others may read your code in the future. So, a variable such as $totalamounttax is very hard to read and should be changed to something like $total_amount_tax or even $TotalAmountTax. I prefer $total_amount_tax as it is the easiest to read quickly when parsing thru your code… Next, if you “define” a constant it is not good programming practice to name it the same as a variable. It is extremely hard to follow that in code. So your line: “$chickenprise = $chickenmasala * CHICKENMASALA ;” does not make sense to anyone reading it. Hard to read.
Please do not be offended by these comments. It is meant to be helpful. When you post code for others to read, they do not want to waste time having to learn what all the constants are just to help with a few bad lines… Hope that made sense. Let us know if you get it solved…

[php]
//tax rate
$taxrate = .05;
$totalamounttax = $totalamount * $taxrate;
$totalamount = $totalamount + $totalamounttax;
echo "total bill amount including tax RS $totalamount [/php]

the code which you provided me i already used that before because its the simple maths so i tried this before
but the problem is when the total amount exceeds 999, the $totalamount shows normal integer but total amount including tax goes in decimal place
example :if i buy 9: CHICKEN MASALA for RS:900
total bill amount excluding tax RS 900.00
total bill amount including tax RS 945<===== everything is fine…but

example2 :10: CHICKEN MASALA for RS:1000
total bill amount excluding tax RS 1,000.00
total bill amount including tax RS 1.05 <=====problem is after exceeding 999 tax result goes in decimal place

[php]its my first time in phphelp forum. I was looking for edit option did not find it for some mistakes i used quote option twice :smiley: am sorry for that [/php]

there should be forum edit option :slight_smile:

Sorry, new members can not edit. That is so spamming software can not update posts.
If you stay in the site as a member and post more, you will be given an edit option.
You can just add another post to your subject and say something like:
EDIT: Just wanted to add this… or correct line 29, or whatever… Works until you become a full member…

Now, onto your code. The problem is that in PHP variables are not “cast” into integer or float formats automatically. So, sometimes you must add the cast’s yourself. So, sometimes you have to tell what each variable actually is such as integer or float. But, in your cast, you can just tell the results to be formatted a certain way. In this case, 2 decimal places and commas at each thousands… This can be done for your use this way:
[php]
$taxrate = .05;
$totalamounttax = $totalamount * $taxrate;
$totalamount = number_format($totalamount + $totalamounttax, 2, ‘.’, ‘,’);
[/php]

This is detailed here: http://php.net/manual/en/function.number-format.php

Hope that does it for you… Let us know…

thanks for ( This is detailed here: http://php.net/manual/en/function.number-format.php)
i got the solution in above link
me dot nosp at m dot unreal4u dot com 08-Jul-2010 12:36
Maybe it is documented, but I just missed it. However, a comment is always useful.

An array will always be converted into 1, so, if you have:

<?php $my_array = array(55); echo number_format($my_array,0,',','.'); // 1 var_dump(number_format($my_array,2,',','.')); // string(4) "1,00" ?>

Since it took me a while to figure it out, I hope this will save somebody’s time.

Greetings !!
isedc at yahoo dot com 29-Mar-2010 12:38
Some programmers may have scripts that use the number_format function twice on a variable. However, if a number is 4 or more digits, using the function twice with only the decimals parameter will lose part of the value that follows the thousands separator.

<?php $var = number_format(2234,2); $var = number_format($var,2); echo $var; # Expected Output: 2,234.00 # Actual Output: 2.00 ?>

To fix, remove the thousands separator by setting the decimal point and thousands separator parameters like so:

<?php $var = number_format(2234,2,'.',''); $var = number_format($var,2,'.',''); echo $var; # Expected Output: 2234.00 # Actual Output: 2234.00 ?>

If it’s 3 digits or less, then it works normally with only the decimals parameter since there is no thousands separator.

<?php $var = number_format(123,2); $var = number_format($var,2); echo $var; # Expected Output: 123.00 # Actual Output: 123.00 ?>

ErnieAlex thanks for the help with coding guidance , i am a new web learner with out any institution support. i follow lots of pdf for practice if you have any idea which pdf to follow it wold be very helpfull,also learning ajax ,jquery, mysql along with php

Your are welcome!

If you are interested in learning more, I would suggest four sites to read.

  1. THIS SITE! LOL, lots of things are discussed here and we have a lot of talented members!
    (You can search the tutorials that have been written by members or search for items you are interested in.)

  2. The actual PHP manual which has all the syntax and samples for every PHP command. When reading this
    site, you need to read down to where samples are posted by PHP programmers.
    www.php.net/manual/

  3. The w3schools site is GREAT for learning just about anything in programming. They have simple tutorials
    that walk you thru tasks step by step. They cover most programming languages.
    http://www.w3schools.com/ (Select a language, search for what you want to learn about.)

  4. Lastly, I suggest this most to programmers. I use it EVERY day!!! Google is your best friend!
    Now, to search Google correctly is the trick. First, if you are looking for a PHP solution, start your query
    with “PHP”. Capitals are not important, but wording is! So, let’s say you want to use PHP to create a
    daily calendar… Search Google using something like this: “PHP open source calendar” or vary the
    query a little like: “php calendar source code”. Some minor change in the query for Google will give you
    a new list of site to view. Most likely you would find it in the first page of the results. I find many solutions
    for members here by Google. I know programming, but, have not programmed everything. Sometimes
    you just need a little help!

Hope this added info steers you to learn more… That is what it is all about… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service