Categories
JavaScript Answers

How to Calculate x% of a Number with JavaScript?

Spread the love

Sometimes, we want to calculate x% of a number with JavaScript.

In this article, we’ll look at how to calculate x% of a number with JavaScript.

Calculate x% of a Number with JavaScript

To calculate x% of a number with JavaScript, we can divide x by 100 then multiply by the number we want to compute the percentage for.

For instance, we can write:

const x = 35.8
const result = (x / 100) * 10000;
console.log(result)

We assign the percentage number to x .

Then we divide that by 100 to get a decimal number with the fraction that we want to multiply the number with.

We multiply that by 10000 to get 35.8% of 10000 and assign that to result .

Therefore, result is 3580.

Conclusion

To calculate x% of a number with JavaScript, we can divide x by 100 then multiply by the number we want to compute the percentage for.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *