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!

ⓘ 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
The End

 

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.

 

QUICK NOTES

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

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();">
  <!-- (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.

 

 

2) TAX CALCULATION WITH JAVASCRIPT

tax-calc.html
<!-- (B) JS CALCULATION -->
<script>
function tcalc(){
  // (B1) GET PRICE + PERCENTAGE
  var price = document.getElementById("taxprice").value,
      percent = document.getElementById("taxperc").value;
  price = parseFloat(price);
  percent = parseFloat(percent);
 
  // (B2) CALCULATE TAX AMOUNT
  var tax = (price / 100) * percent,
      grand = price * ((100 + percent) / 100);
 
  // (B3) ROUND OFF
  // CREDITS - https://www.jacklmoore.com/notes/rounding-in-javascript/
  var roundoff = function (amount, places) {
    let ea = "e" + places,
    eb = "e-" + places;
    return Number(Math.round(amount + ea) + eb);
  };
 
  // (B4) 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 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.

 

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

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *