How to Limit the Amount of Number Shown After a Decimal Place in JavaScript?

Sometimes, we want to limit the amount of number shown after a decimal place in JavaScript.

In this article, we’ll look at how to limit the amount of number shown after a decimal place in JavaScript.

Limit the Amount of Number Shown After a Decimal Place in JavaScript

To limit the amount of number shown after a decimal place in JavaScript, we can use the toFixed method.

For instance, we can write:

const x = 4.8855;
console.log(x.toFixed(2));

Then we round x to 2 decimal places.

The number of decimal places is the argument for toFixed.

Therefore, the console log should log 4.89.

Conclusion

To limit the amount of number shown after a decimal place in JavaScript, we can use the toFixed method.