How do i find the percentage of two numbers

Calculate Percentage between Two Numbers #

To calculate the percentage between two numbers, divide one number by the other and multiply the result by 100, e.g. (30 / 75) * 100. This shows what percent the first number is of the second, in the example - 30 is 40% of 75.

Copied!

function isWhatPercentOf(numA, numB) { return (numA / numB) * 100; } // 👇️ `30` is 40% of `75` console.log(isWhatPercentOf(30, 75)); // 👉️ 40 // 👇️ `20` is 26.666% of `75` console.log(isWhatPercentOf(20, 75)); // 👉️ 26.666666... // ✅ Round to 2 decimals console.log(isWhatPercentOf(20, 75).toFixed(2)); // 👉️ "26.67" // ✅ Get percentage Increase / Decrease function getPercentageIncrease(numA, numB) { return ((numA - numB) / numB) * 100; } // 👇️ `50` is 66.66% increase from `30` console.log(getPercentageIncrease(50, 30)); // 👉️ 66.666 // 👇️ `50` is 50% decrease from `100` console.log(getPercentageIncrease(50, 100)); // 👉️ -50

The first function we declared takes 2 numbers are returns what percent numberA is of numberB.

For example, 25 / 50 * 100 shows that 25 is 50% of 50.

Copied!

// 👇️ `25` is 50% of `50` console.log((25 / 50) * 100);

When calculating percentages between two numbers, you might need to round to a specific number of digits after the decimal.

You can use the Number.toFixed method to achieve this.

Copied!

const percentage = (20 / 75) * 100; console.log(percentage); // 👇️ 26.666666666... // 👇️ 2 decimals const fixed = percentage.toFixed(2); console.log(fixed); // 👉️ "26.67"

The toFixed method formats the number to the provided number of digits after the decimal and rounds the number if necessary.

Note that the toFixed method returns a string, not a number.

If the number doesn't have any decimal places, it gets padded with zeros.

Copied!

const percentage = (50 / 100) * 100; console.log(percentage); // 👇️ 50 // 👇️ 2 decimals const fixed = percentage.toFixed(2); console.log(fixed); // 👉️ "50.00"

Our second function shows how to get the percentage increase or decrease between two numbers.

Copied!

function getPercentageIncrease(numA, numB) { return ((numA - numB) / numB) * 100; } // 👇️ `50` is 66.66% increase from `30` console.log(getPercentageIncrease(50, 30)); // 👉️ 66.666 // 👇️ `50` is 50% decrease from `100` console.log(getPercentageIncrease(50, 100)); // 👉️ -50

The first example shows that the percentage increase from 30 to 50 is 66.6666...%.

And the second example shows that the percentage increase from 100 to 50 is -50%.

Further Reading #

  • How to Convert a Map to JSON in JavaScript
  • Convert an Object to JSON String using JavaScript

How do I find the percentage of 2 numbers?

Percentage Difference Formula The percentage difference between two values is calculated by dividing the absolute value of the difference between two numbers by the average of those two numbers. Multiplying the result by 100 will yield the solution in percent, rather than decimal form.

How do I calculate the percentage of an amount?

If you are required to convert a decimal number like 0.57 to a percentage, you simply multiply it by 100. That is, 0.57 x 100 = 57. Therefore, 0.57 as a percentage equals 57%. Another example is 0.03 x 100 = 3%.