Sometimes, we want to format numbers with JavaScript.
In this article, we’ll look at how to format numbers with JavaScript.
How to format numbers with JavaScript?
To format numbers with JavaScript, we call the number toLocaleString
method.
For instance, we write
const value = (100000).toLocaleString(undefined, { minimumFractionDigits: 2 });
console.log(value);
to call toLocaleString
with { minimumFractionDigits: 2 }
to return a number string with the number rounded to 2 decimal places.
Conclusion
To format numbers with JavaScript, we call the number toLocaleString
method.