Bob Nadler, Jr. Bob Nadler, Jr.

Formatting Currency in JavaScript

Published almost 4 years ago less than 1 min read
5 different coins in 5 different currency units (Japanese yen, Taiwanese dollar, Malaysian ringgit, US cent, Thai baht)
Image by aotaro

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.


Share This Article