- 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!
ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TABLE OF CONTENTS
|Download & Notes||Tax Calculator||Useful Bits & Links|
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
EXAMPLE CODE DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
TAX CALCULATOR DEMO
1) THE HTML
<!-- (A) HTML FORM --> <form onsubmit="return tcalc();"> <!-- (A1) ITEM PRICE --> Price <input type="number" min="0" max="9999" step="0.01" id="taxprice" required value="12.34"/> <br> <!-- (A2) TAX PERCENTAGE --> Tax Percentage <input type="number" min="1" max="100" step="1" id="taxperc" required value="9"/> <br> <!-- (A3) TAXABLE AMOUNT --> Tax Amount <input type="text" readonly id="taxamt"/> <br> <!-- (A4) TOTAL WITH TAX --> Total With Tax <input type="text" readonly id="taxgrand"/> <br> <!-- (A5) 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 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.
USEFUL 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!