Jump to content

**solved**calculate GST PST and Total correctly


Lisawynn

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.