# How To Calculate GST VAT In Javascript (Simple Example)

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!

## JAVASCRIPT TAX CALCULATOR All right, let us now get into the example of a simple VAT/GST calculator with Javascript.

### TAX CALCULATOR DEMO

 Price Tax Percentage Tax Amount Total With Tax

### 1) THE HTML

tax-calc.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>
</td>
</tr>

<!-- (D) TOTAL WITH TAX -->
<tr>
<td>Total With Tax</td>
<td>
</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

tax-calc.js
``````<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 that `round(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. 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.

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. That’s all for this guide, and here is a small section on some extras and links that may be useful to you. 