Categories
JavaScript Answers

How to format numbers and strings with thousand separator with JavaScript?

Spread the love

Sometimes, we want to format numbers and strings with thousand separator with JavaScript.

In this article, we’ll look at how to format numbers and strings with thousand separator with JavaScript.

How to format numbers and strings with thousand separator with JavaScript?

To format numbers with thousand separator with JavaScript, we can use the number toLocaleString method.

For instance, we write

const a = (1234567.89).toLocaleString('en')  
const b = parseFloat("1234567.89").toLocaleString('en')

to call toLocaleString with the locale of the returned formatted number string.

We convert the number string with parseFloat before we call toLocaleString on it since toLocaleString is a number method.

Conclusion

To format numbers with thousand separator with JavaScript, we can use the number toLocaleString 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 *