The other day I needed to format some currency values in JavaScript. I remembered coming across Intl.NumberFormat a long time ago on another project, but browser support was sketchy. I looked up the most recent info on caniuse and thankfully almost all browsers now support it (the only exception is Opera Mini). Let’s take a look at how we can use it for formatting some currency values.
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
formatter.format(4200); /* $4,200.00 */
That's it! Check out the MDN docs for more info on other options that are also available.