esther Joo
esther Joo

Reputation: 459

Calculate percentage increase using math formula

I have a function that calculates percentage increase of 2 numbers:

const 1st_num = 50
const 2nd_num = 100

percentage = ((1st_num - 2nd_num) / 1st_num) * 100  // -100

It seems correct but what if the 1st number is 1?

((1 - 50) / 1) * 100 // -4900

I don't see it making sense anymore. What am I missing?

Upvotes: 2

Views: 1913

Answers (4)

user1099258
user1099258

Reputation:

const fst_num = 70192.32
const snd_num = 17548.08

const percentage = ( 100 - ( ( fst_num - snd_num ) / fst_num ) * 100 );

Upvotes: 0

Pramod S. Nikam
Pramod S. Nikam

Reputation: 4539

First up all your question is more suitable to somewhere in math forums:

Your formula is right just change it as follows to get increase change in positive numbers:

percentage = ((2nd_num - 1st_num) / 1st_num) * 100 // 100%

However your treatment with 1 is exactly right.

4900 % In other words 49 times increase in value.

Upvotes: 2

Allan
Allan

Reputation: 12438

If you are computing a delta variation in percentage between 2 numbers, it should be the other way around:

variation = ((num2 - num1) / num1) * 100

Last but not least, your delta can be over 100%

For example, imagine at

  • t1=10 and t2=11 -> your delta will be computed like this : (11 - 10)/10, so you have an increase of 10%
  • but if you have t1=10 and t2=100 -> your delta will become (100 - 10)/10, so you have an increase of 900%

Upvotes: 2

Nisal Edu
Nisal Edu

Reputation: 7591

You can't use variable name starts with numbers

const fst_num = 50
const snd_num = 100

percentage = ((snd_num -fst_num) / fst_num) * 100  

Upvotes: 1

Related Questions