Emmanuel Akinfulubi

JavaScriptMath

Percentage increase and decrease in JavaScript

Intermediate · 4 min read

A price goes up 20% and then comes down 20%. Most people expect it to be back where it started. It isn't. Percentages are measured against a starting value, and after the rise the starting value is different. Sales reports, discounts, price trackers and growth charts all trip over this.

In this tutorial you will write percentChange(from, to), use it on a rise from 4000 to 5000 and back, and work out exactly what fall undoes a rise. You will add VAT in whole kobo, and then find the two starting values that break the formula: zero and negative numbers. You need to know arrow functions and basic arithmetic.

The percentage change formula

To find how much something changed in percent:

Output

change = (new - old) / old * 100

The important word is old. You always divide by the starting value. From 4000 to 5000 is a change of 1000, and 1000 out of 4000 is 25%.

Why up then down doesn't come back

Going back from 5000 to 4000 is also a change of 1000. But now you divide by 5000, so it is only 20%. The same distance is a different percentage, depending on where you start.

Here is the exercise. Have a go yourself first if you like, then read on and we will build it together.

96 Percentage change, and why up then down does not come back

fair

Write percentChange(from, to) and use it on 4000 to 5000 and back. Show that a 20% rise followed by a 20% fall does not return to the start, work out what fall does undo a 25% rise, and say what your function returns when the starting value is zero or negative.

Stuck? Three hints, smallest first
  1. 1. The change is (to - from) / from, and the denominator is where the trouble is.
  2. 2. To undo a rise of p per cent you need a fall of p / (100 + p) of the new value.
  3. 3. A negative base flips the sign of the answer, so a rise can be reported as a fall.

Step 1: percentChange

Step 1

const percentChange = (from, to) => ((to - from) / from) * 100;
console.log(percentChange(4000, 5000), percentChange(5000, 4000)); // 25 -20

Up 25%, then down 20%, and you are back at 4000. A negative result means a fall.

Step 2: 20% up, then 20% down

Step 2

console.log(4000 * 1.2 * 0.8); // 3840

Multiplying by 1.2 adds 20%, and multiplying by 0.8 takes 20% off. You end at 3840, which is 96% of the start, because 1.2 * 0.8 is 0.96. The 20% fall was taken from the bigger number, 4800, so it removed more than the rise added.

Step 3: the fall that undoes a rise

Step 3

const reverseChange = (p) => 100 / (1 + p / 100) - 100;
console.log(reverseChange(25), reverseChange(-20)); // -20 25

After a rise of p percent, the value is 1 + p / 100 times the start. To get back, you must divide by that, which is the same as keeping 100 / (1 + p / 100) percent of the new value. Take away 100 and you have the change. So a 25% rise needs a 20% fall to undo it, and a 20% fall needs a 25% rise.

Step 4: chained percentages, and float surprises

Step 4

console.log(1.1 * 1.1);       // 1.2100000000000002
console.log(1 - 0.8 * 0.9);   // 0.2799999999999999

Two 10% rises are a 21% rise in total, not 20%: the second 10% is taken from a bigger number. And 20% off followed by 10% off is 28% off, not 30%.

The digits at the end are small errors from how JavaScript stores decimals. Most decimals, like 1.1, can't be stored exactly in binary, so each one is a hair off. Some sums happen to come out exact, like 3840 in Step 2, and others don't. There is no simple rule for which, so check rather than assume.

Step 5: VAT

Nigeria's VAT rate is 7.5%. Adding it means multiplying by 1.075.

Step 5

console.log(12500 * 0.075, 12500 * 1.075); // 937.5 13437.5
console.log(13437.5 / 1.075);               // 12500

On 12,500 naira the VAT is 937.5, for a total of 13,437.5. To take VAT back out of a total, divide by 1.075, not multiply by 0.925. Taking off 7.5% of 13,437.5 would remove too much, for the same reason as in Step 2.

Step 6: money in whole kobo

Prices are safest as whole numbers of the smallest unit. 12,500 naira is 1,250,000 kobo.

Step 6

const vatKobo = (k) => Math.round(k * 0.075);
console.log(vatKobo(1250000), 1250000 + vatKobo(1250000)); // 93750 1343750

Math.round makes sure the VAT is a whole number of kobo, so the total can never pick up a stray fraction.

Step 7: when the start is zero

Step 7

console.log(percentChange(0, 5)); // Infinity
console.log(percentChange(0, 0)); // NaN

Dividing by a starting value of 0 gives Infinity, and 0 to 0 is 0 / 0, which is NaN. Neither throws an error, so they can slip into a report as a blank or a strange figure. Decide what to show instead, for example "new" when the old value was 0.

Step 8: when the start is negative

Step 8

console.log(percentChange(-100, -50)); // -50

A loss shrinking from -100 to -50 is clearly an improvement. But the formula says -50%, which reads like a fall. Dividing by a negative number flips the sign. If your data can go negative (profits, temperatures, account balances), say in words what you mean before computing a percentage. A common fix is to divide by Math.abs(from).

The full solution

The answer panel has percentChange, reverseChange and vatKobo, and prints the rise and fall, the round trip to 3840, the chained changes, the VAT in naira and kobo, and the zero and negative cases. Press Run to see the output.

The answer

const percentChange = (from, to) => ((to - from) / from) * 100;

// The fall that exactly undoes a given rise, as a percentage.
const reverseChange = (p) => 100 / (1 + p / 100) - 100;

// VAT in Nigeria is 7.5%. Held in kobo, it stays an integer.
const vatKobo = (k) => Math.round(k * 0.075);

console.log('4000 -> 5000, and back:', percentChange(4000, 5000), percentChange(5000, 4000));
console.log('up 20 then down 20    :', 4000 * 1.2 * 0.8, 'started at 4000');
console.log('to undo +25 you need  :', reverseChange(25), 'and to undo -20:', reverseChange(-20));
console.log('two 10% rises         :', 1.1 * 1.1, 'against 1.21');
console.log('20% off then 10% off  :', 1 - 0.8 * 0.9, 'against 0.3');
console.log('VAT on 12500 naira    :', 12500 * 0.075, 'total', 12500 * 1.075);
console.log('backing VAT out again :', 13437.5 / 1.075);
console.log('from zero             :', percentChange(0, 5), 'and zero to zero:', percentChange(0, 0));
console.log('from -100 to -50      :', percentChange(-100, -50), '<- that is a rise');
console.log('VAT in kobo           :', vatKobo(1250000), 'total', 1250000 + vatKobo(1250000));

Output


      
9 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 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('4000 up to 5000 is a 25% rise, and back down is a 20% fall', [percentChange(4000, 5000), percentChange(5000, 4000)], [25, -20]);
test('so 20% up then 20% down lands on 3840, not 4000, and exactly so', [4000 * 1.2 * 0.8, 4000 * 1.2 * 0.8 === 3840], [3840, true]);
test('the fall that undoes a 25% rise is 20%, and the rise that undoes it is 25%', [reverseChange(25), reverseChange(-20)], [-20, 25]);
test('two 10% rises are not one 21% rise once doubles are involved', [1.1 * 1.1, 1.1 * 1.1 === 1.21], [1.2100000000000002, false]);
test('and 20% off then 10% off is 28% off, which is not 0.28 either', [1 - 0.8 * 0.9, 1 - 0.8 * 0.9 === 0.28], [0.2799999999999999, false]);
test('7.5% VAT on 12500 naira is 937.5, and dividing by 1.075 gets it back exactly', [12500 * 0.075, 12500 * 1.075, 13437.5 / 1.075], [937.5, 13437.5, 12500]);
test('a base of zero gives Infinity, and zero to zero gives NaN', [percentChange(0, 5) === Infinity, Number.isNaN(percentChange(0, 0))], [true, true]);
test('a negative base inverts the reading: -100 rising to -50 reports as -50%', percentChange(-100, -50), -50);
test('in kobo the VAT is a whole number and the total never grows a fraction', [vatKobo(1250000), 1250000 + vatKobo(1250000), Number.isInteger(vatKobo(1250000))], [93750, 1343750, true]);

Percentages are not additive and they are not symmetric, because each one is measured against a different base. 4000 to 5000 is +25%, 5000 back to 4000 is -20%, and the pair of them is a round trip. A 20% rise then a 20% fall leaves you at 96% of where you started — 3840 on 4000, and that particular arithmetic happens to be exact in doubles. Chained discounts work the same way: 20% off then 10% off is 28% off, never 30%.

The float behaviour is worth checking rather than assuming. 4000 * 1.2 * 0.8 === 3840 is true. 1.1 * 1.1 === 1.21 is false. Same kind of expression, opposite outcome, and there is no rule of thumb that predicts which — only running it tells you. Which is the argument for keeping money in integer kobo: vatKobo returns a whole number and the total cannot drift.

The denominator is the real hazard. A base of zero gives Infinity, and zero to zero gives NaN; neither throws, so both flow into a dashboard as a blank or a nonsense figure. A negative base is worse, because it produces a plausible-looking number with the wrong sign: a loss shrinking from -100 to -50 is unambiguously an improvement, and the formula reports -50%. Any percentage-change code that can see negative or zero bases needs to say what it means, in words, before it computes anything.

Common mistakes

  • Adding percentages together. +20% then -20% is not 0%. Multiply the factors instead: 1.2 * 0.8 is 0.96.
  • Removing VAT by subtracting 7.5%. Divide the total by 1.075 to get the price before VAT.
  • Ignoring a zero or negative start. You get Infinity, NaN or a flipped sign. Handle those cases before dividing.

Try it yourself

  1. A shirt costs 8000 naira. It goes up 15%, then down 15%. What does it cost now?
  2. Write afterChanges(start, percents) that applies a list of changes, like [10, -5, 20], one after another.
  3. Change percentChange to divide by Math.abs(from) and to return null when from is 0. Test it on -100 to -50.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.