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:
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
const firstNumber = 10;
const secondNumber = 20;
const percentage = (firstNumber/secondNumber)*100;
console.log(percentage); //π 50
2. Calculate the percentage of 5 out of 10
const firstNumber = 5;
const secondNumber = 10;
const percentage = (firstNumber/secondNumber)*100;
console.log(percentage); //π 50
3. Calculate 20 out of 100
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.
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.
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
.
Percentage = ((New Number - Old Number) / Old Number) * 100
For example, calculate the percentage increase from 10 to 20.
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.
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:
- Unexpected identifier Error in JavaScript
- Dynamically Access Object Property Using Variable in JavaScript
- Check if a Key exists in an Object in JavaScript
- Replace All String Occurrences in JavaScript
- Get the Last N Elements of an Array in JavaScript
- Remove Special Characters from a String in JavaScript
- Using async/await with a forEach loop
- Wait for all promises to resolve in JavaScript