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.
vat_split(gross: int, rate_percent: int) → Split
Where you start
def vat_split(gross: int, rate_percent: int) -> Split:
Worked examples
| Call | Result |
|---|---|
vat_split(1200, 20) | Split(net=1000, tax=200) |
vat_split(1000, 18) | Split(net=847, tax=153) |
vat_split(1180, 18) | Split(net=1000, tax=180) |
vat_split(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 Python
def vat_split(gross: int, rate_percent: int) -> Split:
d = 100 + rate_percent
net = (gross * 100 + d // 2) // d
return Split(net=net, tax=gross - net)