How to calculate the percentage of a number using JavaScript.

In this tutorial, we will show you how to calculate the percentage of a number using JavaScript.

For example, “What is 50% of 100?” Or “What is 23% of 1000?”

Thankfully, all of this is pretty easy once you know the formula.

Let’s start with an easy example.

Take a look at the following JavaScript code:

//Our number.
var number = 120;

//The percent that we want to get.
//i.e. We want to get 50% of 120.
var percentToGet = 50;

//Calculate the percent.
var percent = (percentToGet / 100) * number;

//Alert it out for demonstration purposes.
alert(percentToGet + "% of " + number + " is " + percent);

//The result: 50% of 120 is 60

In the snippet above, we used a simple formula to calculate what 50% of 120 is.

We divide the percent that we want to get (50) by 100 and then multiply the result of that division (0.5) by the number that we want to get the percent of (120).

Broken down into plain English:

    1. We divide 50 by 100, which is equal to 0.5.
    2. We then multiply that 0.5 figure by 120.
    3. That gives us 60.

As a result, we now know 50% of 120 is 60.

You can also do it this way if it helps you understand the formula a little easier:

//Our number.
var number = 90;

//The percent that we want to get.
//i.e. We want to get 22% of 90.
var percentToGet = 22;

//Turn our percent into a decimal figure.
//22 will become 0.22 and 60 will become 0.6
var percentAsDecimal = (percentToGet / 100);

//Multiply the percent decimal by the number.
var percent = percentAsDecimal * number;

//Alert it out for demonstration purposes.
alert(percentToGet + "% of " + number + " is " + percent);

//The result: 22% of 90 is 19.8

In the snippet above, we split the percentage calculation into two separate lines.

Here, you can see that we convert our percentage value into a decimal by dividing it by 100.

This leaves us with 0.22.

We then calculate the percent by multiplying that decimal number by the number that we want to get the percentage of.

In other words, we multiply 0.22 by 90, which gives us 19.8.

If you run the code above, you will see that the alert box prints out: “22% of 90 is 19.8”.

Custom JavaScript function.

If you are looking for a JavaScript function that you can copy and paste into your project, then you can use the code below:

/**
 * Custom function that calculates the percent of a number.
 * 
 * @param float percent The percent that you want to get.
 * @param float|int num The number that you want to calculate the percent of.
 * @returns {Number}
 */
function calculatePercent(percent, num){
    return (percent / 100) * num;
}

The function above takes in two parameters:

  1. The percentage that you want to find.
  2. The number.

Take a look at the following examples, which will show you how to use this function:

console.log(calculatePercent(2, 100)); //Result: 2
console.log(calculatePercent(33, 100)); //Result: 33
console.log(calculatePercent(20, 1000)); //Result: 200
console.log(calculatePercent(33.3, 1200)); //Result: 399.59999999999997

If you want to remove the extra decimal places from the final figure, then you can take a look at the following guide: Rounding a number to 2 decimal places using JavaScript.