How to Calculate GST Correctly in a Billing App (JavaScript)
If you're building a billing or POS app for Indian businesses, GST looks simple: multiply by the rate. Then real invoices arrive: MRP prices that already include tax, local and inter-state sales, five lines that don't ad
If you're building a billing or POS app for Indian businesses, GST looks simple: multiply by the rate. Then real invoices arrive: MRP prices that already include tax, local and inter-state sales, five lines that don't add up to a round number, and a CA asking why GSTR-1 is off by a few paise.
Here's a small, tested approach that handles the common cases.
The rules your code needs to follow
- Rates: after the September 2025 changes, most goods are at 5% or 18%, with 40% for a small set of luxury and sin goods, plus exempt items.
- Inclusive vs exclusive: retail prices (MRP) include GST; wholesale prices usually exclude it.
- Place of supply: a sale within the seller's state splits GST into CGST + SGST (half each). A sale to another state charges IGST at the full rate.
- Rounding: tax is calculated per line; the invoice total is rounded to the nearest rupee and the difference shown as round-off.
Line-level tax
const round2 = n => Math.round((n + Number.EPSILON) * 100) / 100;
// rate = total GST rate, e.g. 5, 18 or 40
function lineTax({ price, qty, rate, inclusive, sameState }) {
const gross = price * qty;
const taxable = inclusive
? round2(gross * 100 / (100 + rate))
: round2(gross);
if (sameState) {
const cgst = round2(taxable * (rate / 2) / 100);
return { taxable, cgst, sgst: cgst, igst: 0 };
}
return { taxable, cgst: 0, sgst: 0, igst: round2(taxable * rate / 100) };
}
Two details matter here. First, for inclusive prices, compute the taxable value first (price × 100 / (100 + rate)), not price × rate / 100; the second overstates tax. Second, compute CGST and SGST each at half the rate on the taxable value, so they are always equal on the invoice.
Invoice totals with round-off
function billTotals(lines, opts) {
const t = { taxable: 0, cgst: 0, sgst: 0, igst: 0 };
for (const l of lines) {
const r = lineTax({ ...l, ...opts });
for (const k in t) t[k] = round2(t[k] + r[k]);
}
const exact = round2(t.taxable + t.cgst + t.sgst + t.igst);
const total = Math.round(exact);
return { ...t, roundOff: round2(total - exact), total };
}
Test it
const lines = [
{ price: 245, qty: 1, rate: 5 },
{ price: 499, qty: 2, rate: 18 },
{ price: 118, qty: 3, rate: 18 },
];
billTotals(lines, { inclusive: true, sameState: true });
// { taxable: 1379.09, cgst: 108.95, sgst: 108.95, igst: 0,
// roundOff: 0.01, total: 1597 }
billTotals(lines, { inclusive: true, sameState: false });
// { taxable: 1379.09, cgst: 0, sgst: 0, igst: 217.91,
// roundOff: 0, total: 1597 }
The customer pays exactly the MRP total (245 + 998 + 354 = ₹1,597), and the paise difference from rounding sits in a visible round-off line instead of disappearing.
Things to handle next
- Per-item rates from HSN codes, not a single rate per invoice.
- Cess on the few goods that attract it.
- Discounts: apply before tax when given on the invoice.
- Money as integers: for large volumes, store amounts in paise to avoid floating-point drift.
- HSN summary: GSTR-1 needs totals grouped by HSN code and rate.
If you'd rather not build all of this yourself, Vyapsetu is a GST billing app for Indian shops, and its free GST calculator is handy for checking your own numbers.
GST rules change through notifications. Treat this as an engineering pattern and confirm tax rules with a CA.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.