Categories
JavaScript Answers

How to format a rounded number to N decimals with JavaScript?

Spread the love

Sometimes, we want to format a rounded number to N decimals with JavaScript.

In this article, we’ll look at how to format a rounded number to N decimals with JavaScript.

How to format a rounded number to N decimals with JavaScript?

To format a rounded number to N decimals with JavaScript, we can use the number toFixed method.

For instance, we write

const myNumber = 2;
const numStr = myNumber.toFixed(2);

to call myNumber.toFixed with 2 to return myNumber formatted to 2 decimal places as a string.

Conclusion

To format a rounded number to N decimals with JavaScript, we can use the number toFixed method.

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 *