Drill

ProblemsTypeScript › pricing

Split a gross amount into net and VAT

mediumpricingTypeScript

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

vatSplit(gross: int, ratePercent: int) → Split

Solve it in the editor →

Where you start

function vatSplit(gross: number, ratePercent: number): Split {
  
}

Worked examples

CallResult
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 };
}

The same problem in another language

More pricing problems in TypeScript