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
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func vatSplit(gross int, ratePercent int) Split {
}
Worked examples
| Call | Result |
|---|---|
vatSplit(1200, 20) | Split{Net: 1000, Tax: 200} |
vatSplit(1000, 18) | Split{Net: 847, Tax: 153} |
vatSplit(1180, 18) | Split{Net: 1000, Tax: 180} |
vatSplit(999, 0) | Split{Net: 999, Tax: 0} |
Hint
net = (gross * 100 + half of the divisor) / (100 + rate), using integer division. Then subtract.
Reference solution in Go
func vatSplit(gross int, ratePercent int) Split {
d := 100 + ratePercent
net := (gross*100 + d/2) / d
return Split{Net: net, Tax: gross - net}
}