Drill

ProblemsPython › pricing

Split a gross amount into net and VAT

mediumpricingPython

Accounting receives invoice totals that already include VAT and needs the two halves separately.

vat_split(gross: int, rate_percent: int) → Split

Solve it in the editor →

Where you start

def vat_split(gross: int, rate_percent: int) -> Split:
    

Worked examples

CallResult
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)

The same problem in another language

More pricing problems in Python