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
C# 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
public Split VatSplit(int gross, int ratePercent) {
}
Worked examples
| Call | Result |
|---|---|
VatSplit(1200, 20) | new Split(1000, 200) |
VatSplit(1000, 18) | new Split(847, 153) |
VatSplit(1180, 18) | new Split(1000, 180) |
VatSplit(999, 0) | new Split(999, 0) |
Hint
net = (gross * 100 + half of the divisor) / (100 + rate), using integer division. Then subtract.
Reference solution in C#
public Split VatSplit(int gross, int ratePercent) {
int d = 100 + ratePercent;
int net = (gross * 100 + d / 2) / d;
return new Split(net, gross - net);
}