TIL Number formatting with Javascript
POSTED ON:
TAGS: numbers javascript mdn
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!
via How to create a formatted string from a number with the Intl.NumberFormat API
Related TILs
Tagged: numbers