. Advertisement .
..3..
. Advertisement .
..4..
A dynamic computer programming language is known as JavaScript. Its implementations allows the client-side script to interact with users and create dynamic pages, and it is most frequently used as a component of web pages. How to calculate percentage between two numbers in JavaScript is a fairly common question every programmer will encounter. So, what are our options? Everything will be explained to you in this blog. Read on it.
How to calculate percentage between two numbers in JavaScript?
Method 1: Utilize the Number.toFixed method
To calculate percentage between two numbers in JavaScript, you can utilize the Number.toFixed method.
const example_percentage = (19 / 29) * 100;
console.log(example_percentage); // 65.51724137931035
const number_fixed = example_percentage.toFixed(3);
console.log(number_fixed); // 65.517
console.log(typeof(example_percentage)) // number
console.log(typeof(number_fixed)) // string
The number is formatted using the toFixed function to the specified number of digits after the decimal, and it will be rounded if necessary. The toFixed method returns a string, not a number, as you might have seen.
The number is padded with zeros if it has no decimal places.
const example_percentage = (13 / 52) * 100;
console.log(example_percentage); // 25
const number_fixed = example_percentage.toFixed(3);
console.log(number_fixed); // 25.000
The second method demonstrates how to calculate the percentage increase or decrease difference between two numbers.
function cal_percentage(number1, number2) {
return ((number1 - number2) / number2) * 100; }
console.log(cal_percentage(28, 22)); // 27.27272727272727
console.log(cal_percentage(22, 29)); // -24.137931034482758
The first illustration demonstrates that 27.27272727272727… % is the percentage increase from 22 to 28.
The second instance demonstrates that -24.137931034482758…% is the percentage increase from 29 to 22.
Method 2: Make a function called CalculatePercentage()
Make a function called CalculatePercentage() that takes two numbers as inputs, and the percentage of the first number with the second must be returned by it. Two numbers are passed as arguments to the abovementioned CaculatePercentage() function, then show the output. Using the toFixed() method, you can round the percentage to three decimal places—the Program’s Exit.
function CalculatePercentage(first_number, second_number) {
return (first_number/ second_number) * 100;
}
console.log(CalculatePercentage(30, 120).toFixed(3)); // 25.000
Method 3: Divide one number by the other and multiply the result by 100
You have to divide one number by the other and multiply the result by 100 to find the percentage difference between two numbers in JavaScript, for example, (16/64)*100. This illustrates what percentage of the first number is of the second, in this case, 16 is 25% of 64. Look at the following code to understand more:
function Cal_Percentage(x, y) {
return (x / y) * 100;
}
console.log(Cal_Percentage(16, 64)); // 25
console.log(Cal_Percentage(20, 64).toFixed(3)); // 31.250
// Calculate the percentage increase or decrease
function Cal_Percentage_Increase_Ecrease (x, y) {
return ((x - y) / y) * 100;
}
// The percentage increase from 12 to 19 is 58.333333333333336%
console.log(Cal_Percentage_Increase_Ecrease (19, 12)); // 58.333333333333336
// The percentage decrease from 28 to 22 is 21.428571428571427%
console.log(Cal_Percentage_Increase_Ecrease (22, 28)); // -21.428571428571427
The first function we define accepts two numbers as input and returns what the percentage of x is of y.
For instance, 36/72*100 demonstrates that 50% of 72 is 36.
// 36 is 50% of 72
console.log((36 / 72) * 100);
You might need to round to a specified number of digits after the decimal when calculating percentages between two figures.
The solutions described above are very simple, but tthey work flawlessly. They will make your error completely disappear and your program will run well without any errors. So, what are you waiting without applying them for your problem? Let’s try them to get your desired results.
Conclusion
The solution mentioned above is the fastest method for those still confused with the problem How to calculate percentage between two numbers in JavaScript. If you still need support or have any questions related to JavaScript, we have one vibrant community where all members are always willing to assist you with any trouble. Finally, wishing you a more successful day with new approaches or functions and sees you!
Read more
Leave a comment