JavaScript

How to Calculate Percentage Between Two Numbers in JavaScript?

While solving mathematical problems in JavaScript, there can often be a requirement to compute the percentage between two numbers to compare them. For instance, analyzing the difference between one number with respect to another. In such situations, calculating the percentage between two numbers using JavaScript does wonder in analyzing and utilizing the data effectively.

This write-up will explain the procedure to compute the percentage between two numbers in JavaScript.

How to Calculate/Compute Percentage Between Two Numbers in JavaScript?

To calculate the percentage between two numbers in JavaScript, apply the following approaches:

Approach 1: Calculate Percentage Between Two Numbers in JavaScript Using User-defined Function

This approach can be utilized to calculate the percentage between two numbers by passing the numbers that need to be computed as function arguments.

Example

Let’s overview the below example:

<script type="text/javascript">
function getPercent(x, y) {
 return (x / y) * 100;
}
console.log("The percentage between two numbers is:", getPercent(10, 27));
</script>

In the above lines of code:

  • Declare a function named “getPercent()” having the parameters “x” and “y”.
  • In its definition, compute the percentage by dividing the passing numbers and multiplying their result with “100”.
  • Lastly, invoke the particular function by passing the stated numbers as function arguments.
  • The outcome will indicate the percentage of the first passed argument with respect to the passed second argument.

Output

In the above output, it can be visualized that the first passed argument 10 is “37.037%” of the second passed argument 27.

Approach 2: Calculate Percentage Between Two Numbers in JavaScript Using toFixed() Method

The “toFixed()” method transforms a number into a string. This method can be implemented to return the resultant percentage up to two decimal places.

Example

The below-given example explains the stated concept:

<script type="text/javascript">
let getPercent = (25 / 80) * 100;
console.log("The percentage between two numbers to two decimal places is:", getPercent.toFixed(2));
</script>

In the above code snippet:

  • Divide the stated numbers and multiply them by 100.
  • After that, apply the “toFixed()” method to return the corresponding percentage up to two decimal places, as evident in its parameter.
  • Finally, display the resultant percentage of the former number with respect to the latter number, as discussed in the previous approach.

 

Output

In this output, it can be seen that the resultant percentage has two decimal places.

Approach 3: Calculate Percentage Between Two Numbers Using Math.round() Method in JavaScript

The “Math.round()” method rounds the specified number to the closest/nearest integer. This method can be utilized to yield the computed percentage to the nearest integer.

Syntax

Math.round(a)

In the given syntax:

  • a” represents the number that needs to be rounded.

 

Example

The following example illustrates the stated concept:

<script type="text/javascript">
function getPercent(x, y) {
 return Math.round((x / y) * 100);
}
console.log("The percentage between two numbers is:", getPercent(10, 27));
</script>

Implement the below-stated steps, as given in the above code block:

  • Firstly, declare a function named “getPercent()” having the stated parameters.
  • In the function definition, compute the percentage between the passed arguments and apply the “round()” method to them.
  • This will, eventually, return the resultant percentage value to the nearest integer, thereby neglecting the decimal places.

 

Output

The above output signifies that the desired functionality is achieved.

Conclusion

The “User-defined” function, the “toFixed()” method, or the “Math.round()” method are used to compute the percentage between two numbers using JavaScript. These approaches can be utilized to perform the desired functionality upon the passed arguments, returning the outcome up to two decimal places or rounding it to the nearest integer respectively. This article explained the approaches to compute the percentage between two numbers using JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.