DEV Community

Duc Anh Phung
Duc Anh Phung

Posted on

Building a Vietnamese payroll calculator: the 4 rules that trip up developers

If you're building HR or payroll software for Vietnam, the gross-to-net calculation looks simple until it isn't. Four rules cause most of the bugs I've seen.

1. Insurance uses a different base than tax

Employee social, health and unemployment insurance are calculated on the insured salary, which has a floor (regional minimum wage) and a ceiling. Tax is calculated on income after insurance. Two different bases, so keep two fields.

2. Deductions come before brackets

Taxable income = gross − employee insurance − personal deduction − dependant deductions − exempt allowances. Only registered dependants count.

3. Brackets are progressive, slice by slice

Apply each rate only to the slice within its bracket. Applying the top marginal rate to everything is the classic bug.

function pit(taxable, brackets) {
  let tax = 0, prev = 0;
  for (const { upTo, rate } of brackets) {
    if (taxable <= prev) break;
    tax += (Math.min(taxable, upTo) - prev) * rate;
    prev = upTo;
  }
  return Math.round(tax);
}
Enter fullscreen mode Exit fullscreen mode

Keep brackets, deductions and insurance caps in dated config: they change by law, and Vietnam's PIT rules were revised for 2026.

4. Residents vs non-residents

Non-residents pay a flat rate on Vietnam-sourced income with no deductions. Branch early.

Test against a reference

Pick 10 salaries (including one above the insurance cap and one with dependants) and compare your output to an independent tool. I use VNEN's PIT calculator for spot checks before each release.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

Rule 1 is the one that would have bitten me — two different bases for insurance and tax is exactly the kind of thing that looks right on a single test salary and only breaks at the cap. The dated-config advice is the part I would steal: rules that change by law should never live as magic numbers in code. One question: when you ran your 10-salary reference set, which rule produced the biggest discrepancy — the insurance ceiling, or the resident versus non-resident branch? And do you round at each step or only at the end? Rounding is where my own pay math has quietly drifted before.