Calculate Percentage Between Two Numbers in JavaScript

There are many situations in programming where we need to calculate percentages such as analysing the differnce between one number with respect to another.

In this post, we will learn how to calculate percentage between two number in JavaScript. In addition, we will calculate percentage increase or decrease between two numbers.

How to calculate percentage?

When calculating the percentage between two numbers in JavaScript, the formula is as follows:

Formula
Percentage = (First Number/Second Number) * 100

This formula will give you the percentage of the first number out of the second number.

Let’s look at a few examples of calculating the percentage between two numbers.

1. Calculate the percentage of 10 out of 20

JavaScript
const firstNumber = 10;
const secondNumber = 20;

const percentage = (firstNumber/secondNumber)*100;

console.log(percentage); //πŸ‘‰ 50

2. Calculate the percentage of 5 out of 10

JavaScript
const firstNumber = 5;
const secondNumber = 10;

const percentage = (firstNumber/secondNumber)*100;

console.log(percentage); //πŸ‘‰ 50

3. Calculate 20 out of 100

JavaScript
const firstNumber = 20;
const secondNumber = 100;

const percentage = (firstNumber/secondNumber)*100;

console.log(percentage); //πŸ‘‰ 20

If you want to round the percentage to the nearest integer, use the Math.round() method.

JavaScript
const firstNumber = 15.2;
const secondNumber = 100;

const percentage = (firstNumber/secondNumber)*100;

console.log(percentage); //πŸ‘‰ 15.2

const roundedPercentage = Math.round(percentage);

console.log(roundedPercentage); //πŸ‘‰ 15

As you can see, it returns 15 as its nearest integer from 15.2, which is the calculated percentage.

Same way, use the toFixed() method to round the decimal places.

The toFixed() method takes an integer argument that specifies the number of decimal places to use. The number is rounded to this decimal place.

JavaScript
const percentage = (40 / 90) * 100;
console.log(percentage); // πŸ‘‡οΈ 44.444...

// πŸ‘‡οΈ 2 decimals
const fixed = percentage.toFixed(2);
console.log(fixed); // πŸ‘‰οΈ "44.44"

⚠️ Note that the toFixed method returns a string, not a number. And If the number has no decimal points, it gets padded with zeros.


How to calculate percentage increase or decrease between two numbers?

To calculate percentage increase or decrease between two numbers we can use math formula like we used to calculate percentage between two numbers.

The formula is to subtract newNumber from oldNumber, divide the result by oldNumber, and multiply this result by 100.

Formula
Percentage = ((New Number - Old Number) / Old Number) * 100

For example, calculate the percentage increase from 10 to 20.

JavaScript
const oldNumber = 10;
const newNumber = 20; 

const percentage = ((newNumber - oldNumber) / oldNumber) * 100;

console.log(percentage); // πŸ‘‰ 100

It returns a positive number if the percentages increase; otherwise, it returns a negative number.

For example, calculate the percentage decrease from 20 to 10.

JavaScript
const oldNumber = 20;
const newNumber = 10; 

const percentage = ((newNumber - oldNumber) / oldNumber) * 100;

console.log(percentage); // πŸ‘‰ -50

It returns negative number means the percentage decrease from 20 to 10 is 50%.


Conclusion

In conclusion, calculating the percentage between two numbers in JavaScript is simple. With the right formula, we can quickly and easily calculate the percentage.


Learn More:

Unlock your programming potential with Stack Thrive! Discover step-by-step tutorials, insightful tips, and expert advice that will sharpen your coding skills and broaden your knowledge.

Leave a Comment

Facebook Twitter WhatsApp