Jump to content

Archived

This topic is now archived and is closed to further replies.

Lisawynn

**solved**calculate GST PST and Total correctly

Recommended Posts

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
[code]
td {
    border-width: 1px;
    border-style: solid;
    border-color: #000000;
}

th {    border-width: 1px;
    border-style: solid;
    border-color: #000000;
}
[/code]

Inv2.html
[code]
<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>
[/code]

Calc4.php
[code]
<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>
[/code]

Thank you!!!

Share this post


Link to post
Share on other sites
It's because you print rounded values but use non-rounded ones in the calculations. Round then print.

Try this for calc4.php

[code]<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>[/code]

Share this post


Link to post
Share on other sites
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!!!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.