- To get the taxable amount for an item (or total) –
var tax = (PRICE / 100) * TAX PRECENTAGE;
- To quickly calculate the grand total with tax –
var grandWithTax = TOTAL * ((100 + TAX PERCENTAGE) / 100);
Yep, it’s that simple. But let us walk through a simple tax calculator example in this guide – Read on!
TABLE OF CONTENTS
TAX CALCULATOR DEMO
1) THE HTML
<!-- (A) HTML FORM --> <form onsubmit="return tcalc();"> <table> <!-- (A) ITEM PRICE --> <tr> <td>Price</td> <td> <input type="number" min="0" max="9999" step="0.01" id="taxprice" required value="12.34"> </td> </tr> <!-- (B) TAX PERCENTAGE --> <tr> <td>Tax Percentage</td> <td> <input type="number" min="1" max="100" step="1" id="taxperc" required value="9"> </td> </tr> <!-- (C) TAXABLE AMOUNT --> <tr> <td>Tax Amount</td> <td> <input type="text" readonly id="taxamt"> </td> </tr> <!-- (D) TOTAL WITH TAX --> <tr> <td>Total With Tax</td> <td> <input type="text" readonly id="taxgrand"> </td> </tr> </table> <!-- (E) GO! --> <input type="submit" value="Go!"> </form>
Yes, there’s nothing “special” here. Just an HTML form with 4 fields – The price, tax percentage, taxable amount, and total after tax.
As in the introduction above, all this script does is:
- (B1) Get the price and tax amount from the HTML fields.
- (B2) Calculate the taxable amount and grand total.
- (B3) Roundoff.
- (B4) Set the results into the taxable amount and grand total fields.
EXTRA) BE CAREFUL WITH ROUNDING OFF!
Please take note that the above example rounds off to 2 decimal places (Captain Obvious to the rescue). But depending on the region and company, the requirements for rounding off can be very different. For example, there are no decimal places for the Japanese Yen, so it might be floor or ceiling to the next nearest whole number:
Math.ceil()Always round up to the nearest whole number.
Math.floor()Always round down to the nearest whole number.
Math.round()Will round off to the nearest integer. But it can be rather funky… Credits to Jack Moore, and read more on his blog if you want to know why we have to do that
round(amount + eN) + e-Nthing.
Also, some companies may have a very specific rule to round off to the nearest 5 cents.
- 0.01 to 0.24- Round down to 0.
- 0.25 to 0.49 – Round up to 0.5.
- 0.51 to 0.74 – Round down to 0.5.
- 0.75 to 0.99 – Round up to 1.
It is best to check with a local accountant or whoever knows best.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for this guide, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- Math.ceil – MDN
- Math.floor – MDN
- Math.round – MDN
- If you need more crazy Math – Check out the math.js library.
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!