DEV Community

Vitor Freitas
Vitor Freitas

Posted on

3 1

Formatting currencies with Intl.NumberFormat

Do you know the Intl object in Javascript?

This namespace stands for the ECMAScript's internationalization API, and provides some nice features such as string comparison, and number, date and time formatting. With this post I'll show about currency formatting using the NumberFormat constructor.

Recently I had to format integers to a currency format and I had 2 options:

  • Do it myself (which I'm a huge fan of)
  • Use some external library

But this time was different, and I took some time to search about the topic and then I found Intl.NumberFormat. It's usage is simple, here's an example:

const reaisFormatter = new Intl.NumberFormat('pt-BR', {
  style: 'currency',
  currency: 'BRL'
})

reaisFormatter.format(123.50) // R$ 123,50

And that was it. Simple, huh? And within the NumberFormat constructor you have an infinity of possibilities, such as unit formatting (liters, centimeters, foot...), percentage, decimal formatting and so on.

Perheps you don't need to npm install something to do that anymore. Hope you enjoyed it!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay