Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created July 2, 2023 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/39a0cc628dbf4952c8023fcb220a0898 to your computer and use it in GitHub Desktop.
Save code-boxx/39a0cc628dbf4952c8023fcb220a0898 to your computer and use it in GitHub Desktop.
Calculate GST VAT In Javascript

JAVASCRIPT CALCULATE GST VAT

https://code-boxx.com/calculate-gst-vat-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
table {
border-collapse: collapse;
margin-bottom: 10px;
}
table td {
padding: 10px;
border: 1px solid #ddd;
}
table input {
width: 100%;
padding: 5px;
}
<!DOCTYPE html>
<html>
<head>
<title>Simple Tax Calculator</title>
<meta charset="utf-8"?
<link rel="stylesheet" href="tax-calc.css">
<script src="tax-calc.js"></script>
</head>
<body>
<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>
</body>
</html>
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 = roundoff(tax, 2);
document.getElementById("taxgrand").value = roundoff(grand, 2);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment