Every big social media sites format their numbers the same way like 93.1K followers 1M subscribers or 2.5B views, they do this because it’s much easier to design a UI with a compact number.
To format numbers like this you might think you need to write a function with a bunch of conditional logic or find a package on npm to do it for you. But actually you can do it natively in javascript with the intel or internationalization api it has excellent browser support.
Create a number formatter that first takes the locale language (“en”) then an object with a bunch of options like notation compact which will round the number and replace the last digits with a K M B or T to easily giving you the social media style format
There’s even experimental support for different rounding modes the bottom line is that if you’re ever formatting numbers on a website the internationalization api is likely the tool you’re looking for.
If you found this article useful make sure to follow me for more interesting small programming hacks.
Twitter Handle : AmSorry
Top comments (0)