Problems › TypeScript › pricing
Split a gross amount into net and VAT
Accounting receives invoice totals that already include VAT and needs the two halves separately.
- Everything is in minor units.
- Net is the gross divided by (100 + rate) percent, rounded half up.
- Tax is whatever is left over, so net and tax always add back to the gross exactly.
vatSplit(gross: int, ratePercent: int) → Split
Where you start
function vatSplit(gross: number, ratePercent: number): Split {
}
Worked examples
| Call | Result |
|---|---|
vatSplit(1200, 20) | {"net":1000,"tax":200} |
vatSplit(1000, 18) | {"net":847,"tax":153} |
vatSplit(1180, 18) | {"net":1000,"tax":180} |
vatSplit(999, 0) | {"net":999,"tax":0} |
Hint
net = (gross * 100 + half of the divisor) / (100 + rate), using integer division. Then subtract.
Reference solution in TypeScript
function vatSplit(gross: number, ratePercent: number): Split {
const d = 100 + ratePercent;
const net = Math.floor((gross * 100 + Math.floor(d / 2)) / d);
return { net, tax: gross - net };
}