DEV Community

Gaute Meek Olsen
Gaute Meek Olsen

Posted on • Edited on • Originally published at gaute.dev

Formatting large numbers in JavaScript

How to get a formatted number easily in JavaScript that is human-readable with Intl.NumberFormat.

Let's say we want to display three million, 3000000. This isn't that easy to read, you need to look closely to check that it isn't 300,000 or 30,000,000.

const number = 3000000;
const numberToDisplay = new Intl.NumberFormat().format(number)
Enter fullscreen mode Exit fullscreen mode

Bonus, it will be formatted according to the users' local settings. For me in Norway, numberToDisplay is 3 000 000. While 'en-US' it will be 3,000,000 and 'de-DE' it is 3.000.000

Top comments (1)

Collapse
 
toddalbert profile image
Todd Albert

Even better:
const formatter = Intl.NumberFormat('en', { notation: 'compact' });
And then you can use
const numberToDisplay = formatter.format(number)
You'll get numbers like 1.7 T or 420 M.