Welcome to a quick tutorial on how to calculate tax (GST or VAT) in Javascript. So you may have a certain e-commerce or accounting website that requires the use of Javascript to calculate the GST/VAT.
Calculating the tax amount in Javascript involves some simple Math.
- 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
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short 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.
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.
JAVASCRIPT TAX CALCULATOR
All right, let us now get into the example of a simple VAT/GST calculator with Javascript.
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.
2) TAX CALCULATION WITH JAVASCRIPT
<script>
function tcalc () {
// (A) ITEM PRICE + TAX PERCENTAGE
var price = parseFloat(document.getElementById("taxprice").value),
percent = parseFloat(document.getElementById("taxperc").value);
// (B) CALCULATE TAX AMOUNT
var tax = (price / 100) * percent,
grand = price * ((100 + percent) / 100);
// (C) ROUND OFF
// CREDITS - https://www.jacklmoore.com/notes/rounding-in-javascript/
var roundoff = (amount, places) => {
let ea = "e" + places,
eb = "e-" + places;
return Number(Math.round(amount + ea) + eb);
};
// (D) SET CALCULATED VALUES TO HTML FIELDS
document.getElementById("taxamt").value = tax;
document.getElementById("taxgrand").value = grand;
return false;
}
</script>
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 thatround(amount + eN) + e-N
thing.
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.
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.
THE END
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!