Jump to content


Photo

**solved**calculate GST PST and Total correctly


  • Please log in to reply
2 replies to this topic

#1 Lisawynn

Lisawynn
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 April 2006 - 07:48 PM

Hi. I have recently started learning PHP. I am playing with an invoice and I want the total to add up correctly. I have found that most of the time it does but some of the time, it does not. For example, I am working with 3 files (an html, css and php file - I have included the code for all three below)

When you open the file Inv2.html and input "10" for Quantity and "23.61" for Unit Price (for example) and click the "calculate" button, the total of the invoice should be calculated. However, if you manually add up the numbers that appear in the text boxes (236.10, 16.53 and 16.53), the total should be 269.16 and not 269.15 as is displayed. I have tried using round but that rounds the total down to 269.00. Why is it not calculating based on 16.53+16.53+236.10? It is displaying 16.53 for the tax but is using the true number 16.527 for the calculation. Thanks for any assistance.


border.css
td {
    border-width: 1px;
    border-style: solid;
    border-color: #000000;
}

th {    border-width: 1px;
    border-style: solid;
    border-color: #000000;
}

Inv2.html
<html>
<head>
<title>Invoice</title>
<link rel="stylesheet" type="text/css" href="border.css" />
</head>
<body>
<form method="post" action="Calc4.php">
<table border=0 width="100%">
    <tr>
        <th>Item</th>
        <th>Catalogue#</th>
        <th>Description</th>
        <th>Quantity</th>
        <th>Unit Price</th>
        <th>Cost</th>
    </tr>
    <tr>
        <td><input type="text" name="item" value="">
        </td>
        <td><input type="text" name="cat" value="">
        </td>
        <td>
        <input type="text" name="desc" value="">
        </td>
        <td><input type="text" name="qty" value="">
        </td>
        <td><input type="text" name="price" value="">
        </td>
        <td><input type="text" name="cost" value="">
        </td>
    </tr>
</table>
    <input type="submit" value="calculate">
</form>
</html>

Calc4.php
<html>
<head>
<title>Invoice</title>
<link rel="stylesheet" type="text/css" href="border.css" />
</head>
<body>
<form>
<table border=0 width="100%">
    <tr>
        <th>Item</th>
        <th>Catalogue#</th>
        <th>Description</th>
        <th>Quantity</th>
        <th>Unit Price</th>
        <th>Cost</th>
    </tr>
    <tr>
        <td> <?print $item?></td>
        <td> <?print $cat?></td>
        <td> <?print $desc?></td>
        <td> <?print $qty?></td>
        <td> <?print $price?></td>
        <td>$<?$cost=$qty*$price;
        printf("%1\$.2f", round($cost, 2))?></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td colspan="2">Subtotal</td>
        <td>$<?printf("%1\$.2f",$qty*$price)?></td>
    </tr>
    <tr>
        <td colspan="2"></td>
        <td></td>
        <td colspan="2">GST (7%)</td>
        <td>$<?$GST=$cost*.07;
        printf("%1\$.2f",$GST)?></td>
    </tr>
    <tr>
        <td colspan="2"></td>
        <td></td>
        <td colspan="2">PST (7%)</td>
        <td>$<?$PST=$cost*.07;
        printf("%1\$.2f",$PST)?></td>
    </tr>
    <tr>
        <td colspan="2"></td>
        <td></td>
        <td colspan="2">Total</td>
        <td>$<?printf ("%1\$.2f",($cost+$GST+$PST))?></td>
</table>    
</form>
</html>

Thank you!!!

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 13 April 2006 - 08:49 PM

It's because you print rounded values but use non-rounded ones in the calculations. Round then print.

Try this for calc4.php

<html>
<head>
<title>Invoice</title>
<link rel="stylesheet" type="text/css" href="border.css" />
</head>
<body>
<form>
<? extract ($_POST);?>
<table border=0 width="100%">
    <tr>
        <th>Item</th>
        <th>Catalogue#</th>
        <th>Description</th>
        <th>Quantity</th>
        <th>Unit Price</th>
        <th>Cost</th>
    </tr>
    <tr>
        <td> <?print $item?></td>
        <td> <?print $cat?></td>
        <td> <?print $desc?></td>
        <td> <?print $qty?></td>
        <td> <?print $price?></td>
        <td>$<?$cost=round($qty*$price, 2);
        printf("%1\$.2f", $cost)?></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td colspan="2">Subtotal</td>
        <td>$<?printf("%1\$.2f",$cost)?></td>
    </tr>
    <tr>
        <td colspan="2"></td>
        <td></td>
        <td colspan="2">GST (7%)</td>
        <td>$<?$GST= round($cost*.07, 2);
        printf("%1\$.2f",$GST)?></td>
    </tr>
    <tr>
        <td colspan="2"></td>
        <td></td>
        <td colspan="2">PST (7%)</td>
        <td>$<?$PST=round($cost*.07,2);
        printf("%1\$.2f",$PST)?></td>
    </tr>
    <tr>
        <td colspan="2"></td>
        <td></td>
        <td colspan="2">Total</td>
        <td>$<?printf ("%1\$.2f",($cost+$GST+$PST))?></td>
</table>
</form>
</html>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Lisawynn

Lisawynn
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 April 2006 - 09:00 PM

Thank you!! That worked great. I had tried using round but was putting it in the wrong place (newbie newbie newbie). I appreciate your assistance. Thanks so much!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users