Calculate VAT (7.5%) on a price in JavaScript
Beginner · 4 min read
VAT (value added tax) is the tax added to the price of most goods and services. In Nigeria the rate is 7.5%. Every invoice, receipt and online checkout in the country has to work out the tax on a price, and many also have to go the other way: take a price that already includes VAT and find the price before tax.
In this tutorial you will write three small functions: vatOn for the tax, grossOf for the price with tax, and netFromGross to get back to the price before tax. All amounts are in kobo (100 kobo make one naira), stored as whole numbers so the math stays exact. You need to know what a function is and what Math.round does.
Two words you need: net and gross
- The net price is the price before tax.
- The gross price is the price after tax: net plus VAT.
So on a ₦2,500 item, the net price is ₦2,500, the VAT is 7.5% of that, and the gross price is the two added together.
Percentages as decimals
7.5% means 7.5 out of every 100, which is 0.075 as a decimal. To get 7.5% of a number, multiply by 0.075:
console.log(250000 * 0.075); // 18750
250,000 kobo is ₦2,500, and 18,750 kobo is ₦187.50. Most prices won't come out to a whole number of kobo, so the result needs rounding.
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
64 VAT at 7.5%
easyAdd Nigerian VAT of 7.5% to a price held in kobo, then recover the net price from a gross one. Work in integers throughout.
Stuck? Three hints, smallest first
- 1. VAT is
Math.round(kobo * 0.075). Round once, at the end. - 2. Going backwards from a gross figure is division by
1.075, not subtracting 7.5%. - 3. Try charging VAT per line and then on the total of those lines, and compare.
Step 1: the VAT on a price
Keep the rate in one named constant and round the tax to a whole kobo:
const RATE = 0.075;
const vatOn = (kobo) => Math.round(kobo * RATE);
console.log(vatOn(250000)); // 18750
console.log(vatOn(999)); // 75
console.log(vatOn(0)); // 0
999 kobo times 0.075 is 74.925. Math.round turns that into 75, the nearest whole kobo. Rounding (rather than cutting off the decimals) means a fraction of a kobo goes to whichever whole kobo is closer.
Step 2: the gross price
The gross price is the net price plus its VAT:
const RATE = 0.075;
const vatOn = (kobo) => Math.round(kobo * RATE);
const grossOf = (kobo) => kobo + vatOn(kobo);
console.log(grossOf(250000)); // 268750
That is ₦2,687.50 for a ₦2,500 item.
Step 3: the wrong way back to net
Now you have a gross price of 268,750 kobo and you want the net price. The natural guess is "take 7.5% off":
const RATE = 0.075;
const vatOn = (kobo) => Math.round(kobo * RATE);
console.log(268750 - vatOn(268750)); // 248594
That should be 250,000, and it is 1,406 kobo short. The VAT was 7.5% of the net price, the smaller number. Taking 7.5% of the bigger gross price takes off too much.
Step 4: divide by 1.075
The gross price is the net price times 1.075 (all of the net, plus 0.075 of it). To undo a multiplication, divide:
const RATE = 0.075;
const netFromGross = (gross) => Math.round(gross / (1 + RATE));
console.log(netFromGross(268750)); // 250000
1 + RATE is 1.075. The result is rounded to a whole kobo, like every other amount here.
Step 5: round per line or on the total?
A receipt has several lines. You can work out VAT on each line and add the results, or add the lines and work out VAT once on the total. For three items of ₦10 (1,000 kobo) both give the same answer:
const vatOn = (kobo) => Math.round(kobo * 0.075);
const lines = [1000, 1000, 1000];
const perLine = lines.reduce((sum, k) => sum + vatOn(k), 0);
const onTotal = vatOn(lines.reduce((a, b) => a + b, 0));
console.log(perLine, onTotal); // 225 225
console.log(vatOn(10) * 3, vatOn(30)); // 3 2
But take three items of 10 kobo. Each one has 0.75 kobo of VAT, which rounds up to 1, so per line the VAT is 3 kobo. On the total of 30 kobo the VAT is 2.25, which rounds to 2. Rounding each part and then adding doesn't always match adding and then rounding. Choose one place to round and make every report use the same one, so an invoice and its summary never disagree.
The full solution
The answer panel has all three functions, prints the VAT, gross and net prices for ₦2,500, and compares per-line VAT with VAT on the total. Its checks include the wrong subtraction and the two awkward amounts. Press Run to see the output.
const RATE = 0.075;
const vatOn = (kobo) => Math.round(kobo * RATE);
const grossOf = (kobo) => kobo + vatOn(kobo);
const netFromGross = (gross) => Math.round(gross / (1 + RATE));
const lines = [1000, 1000, 1000]; // three items at 10 naira
const perLine = lines.reduce((sum, k) => sum + vatOn(k), 0);
const onTotal = vatOn(lines.reduce((a, b) => a + b, 0));
console.log('VAT on 2,500 naira ->', vatOn(250000), 'kobo');
console.log('gross ->', grossOf(250000), 'kobo');
console.log('back to net ->', netFromGross(268750), 'kobo');
console.log('VAT per line', perLine, 'vs VAT on the total', onTotal);
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('VAT on 2,500 naira is 187.50', vatOn(250000), 18750);
test('the gross price', grossOf(250000), 268750);
test('and the net price comes back', netFromGross(268750), 250000);
test('subtracting 7.5% from the gross would be wrong', 268750 - vatOn(268750), 248594);
test('a fraction of a kobo is rounded, not dropped', vatOn(999), 75);
test('per-line VAT and VAT on the total need not agree', [perLine, onTotal], [225, 225]);
test('but they part company on awkward amounts', [vatOn(10) * 3, vatOn(30)], [3, 2]);
test('zero attracts none', vatOn(0), 0);
Two mistakes this exercise exists to prevent. Taking 7.5% off a gross price does not give the net price — the fourth assertion is 248,594 kobo where the right answer is 250,000, because the VAT was a percentage of the smaller number. Divide by 1.075 instead.
And rounding is not distributive. Three items of 10 kobo attract 1 kobo of VAT each by line, but 2 kobo on the total, so the invoice and the summary disagree by a kobo. Pick one place to round, write it down, and make every report use the same one; tax authorities care about this more than you would like.
Common mistakes
- Subtracting 7.5% from a gross price. That gives too little. Divide by 1.075 instead.
- Rounding in more than one place. Round per line or on the total, not a mix, or the totals drift apart by a kobo.
- Writing
7.5instead of0.075.kobo * 7.5is seven and a half times the price, not 7.5% of it.
Try it yourself
- Work out the gross price of a ₦12,000 item, in kobo.
- Write
vatFromGross(gross)that returns just the VAT part of a gross price, usingnetFromGross. - Change
RATEto 0.2 (a 20% tax) and check thatnetFromGross(grossOf(500000))still returns 500000.