Today I Learned - Rocky Kev

TIL Number formatting with Javascript

POSTED ON:

TAGS:

Say you wanted to convert this into a string, with all the commas and everything.

You can use the browser Intl package to use this.

let num = 1234567890987654321;

let str = Intl.NumberFormat('en-US').format(num);
// results: '1,234,567,890,987,654,400'

But some countries format numbers differently. Spain uses periods instead of commas.

let num = 1234567890987654321;

let str = Intl.NumberFormat('en-ES').format(num);
// returns '1.234.567.890.987.654.400'

If you leave NumberFormat(undefined), it'll default to the user!

MDN - intl

via How to create a formatted string from a number with the Intl.NumberFormat API


Related TILs

Tagged:

TIL multiple ways to deal with extra 0s

Dealing with lots of 0`s.

TIL Number formatting with Javascript

Say you wanted to convert this into a string, with all the commas and everything. You can use the browser Intl package to use this.

TIL Number formatting with Javascript

Say you wanted to convert this into a string, with all the commas and everything. You can use the browser Intl package to use this.