DEV Community

Daniel Cruz
Daniel Cruz

Posted on

Using Intl.NumberFormat to transform number notation

Problem

You want to transform thousand numbers into their compact version (1,000 -> 1k).

Solution

Use Intl.NumberFormat with the compact notation as option

const formatter = new Intl.NumberFormat("en-US", { notation: "compact" });

const number = 12_000;

const formattedNumber = formatter.format(number) // -> will return '12k'
Enter fullscreen mode Exit fullscreen mode

MDN Reference

Top comments (0)