# Javascript calculate percentage – How to Calculate Percentage Between Two Numbers in JavaScript?

Javascript calculate percentage: To compute the percentage of two numbers, divide one by the other and multiply the result by 100,

For Example:

Here Let us calculate 20% of 400 as follows:

(80 / 400) * 100.

This displays what percentage of the first number is of the second, in this case, 80 is 20% of 400.

## Calculating Percentage Between Two Numbers in JavaScript

index.js:

Approach:

• Create a function say caculatePercentage() which accepts two numbers as arguments and returns the percentage of the first number with the second number.
• Pass two numbers as arguments to the above caculatePercentage() function and print the result
• Here it indicates 80 is 20% of 400
• Similarly, check for the other numbers and print the result
• Rounding the percentage up to 3 decimal places using the toFixed() function.
• The Exit of the Program.

Below is the implementation:

// Create a function say caculatePercentage which accepts
// two  numbers as arguments and returns the percentage of
// first number with the second number
function caculatePercentage(number1, number2) {
return (number1 / number2) * 100;
}

// Pass two numbers as arguments to the above
// caculatePercentage function and print the result
// Here it indicates 80 is 20% of 400
console.log(caculatePercentage(80, 400));

// Similarly check for the other numbers and print the result
console.log(caculatePercentage(40, 150));

// Rounding the percentage upto to 3 decimal places using the toFixed() function
console.log(caculatePercentage(50, 200).toFixed(3));

Output:

20
26.666666666666668
25.000

### Calculate Percentage Increase Or Decrease

Formula:

((number1 - number2) / number2) * 100

Approach:

• Create a function say percentIncreaseOrDecrease which accepts two numbers as arguments and returns the percentage increase or decrease
• Pass two numbers as arguments to the above percentIncreaseOrDecrease function and print the result
• Here it indicates that 150 is 87.5% increase from 80.
• Similarly, check for the other numbers and print the result
• Here it indicates that 60 is 50% decrease from 120.
• The Exit of the Program.

Below is the implementation:

// Create a function say percentIncreaseOrDecrease which//accepts two  numbers as arguments and returns the //percentage increase or decrease
function percentIncreaseOrDecrease(number1, number2) {
return ((number1 - number2) / number2) * 100
}
// Pass two numbers as arguments to the above
// percentIncreaseOrDecrease function and print the result
// Here it indicates 150 is 87.5% increase from 80
console.log(percentIncreaseOrDecrease(150, 80));
//Similarly check for the other numbers and print the result
//Here it indicates 60 is 50% decrease from 120
console.log(percentIncreaseOrDecrease(60, 120));


Output:

87.5
-50

toFixed() Function:

The toFixed() method formats the number after the decimal to the specified
number of digits and rounds it if necessary.
• It should be noted that the toFixed method returns a string rather than a number.
• If the number has no decimal places, it is padded with zeros.

Using toFixed() Function for padding with zeros if has no decimal places

// Calculate the percentage of two numbers using the above
// given mathematical formula and store it in a variable
const rslt_percent = (100 / 200) * 100;
// Print the percentage of first number with the second
console.log(rslt_percent);

// Round Off the aboveresult percentage to 3 decimal places
// using the toFixed() function and store it in another variable
const fixed_percent = rslt_percent.toFixed(3);
// Print the percentage after rounding off to 3 decimals
// Here the result 50.000% is padded with 3 zeros since there are no decimal values for it
console.log(fixed_percent);


Output:

50
50.000