How to format numbers as currency strings

Matthew C.

The Problem

You want to format a number as a currency string to display to a user. How do you do this?

The Solution

You can create an Intl.NumberFormat object to format a number as a currency string. To create the object, call the Intl.NumberFormat() constructor. This object allows for language-sensitive number formatting.

The Intl.NumberFormat() constructor has two optional arguments: locales and options. The locales argument is a language tag string or an array of language tag strings. If you don’t pass in a locales string, the browser detects the preferred language of the user. The options object has multiple properties that can be set. For creating a currency string, set the formatting style property to “currency” and set the currency property to the ISO 4217 currency code for the currency. You can find a list of the currency codes here.

Here’s an example of how to format a number as a currency string:

const { format } = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', }); console.log(format(101.04)); // $101.04 console.log(format(99.99)); // $99.99

You can also set the currencyDisplay property of the options object to change how the currency is displayed. The default value is “symbol” but if you change it to “name”, the localized currency name is displayed. You can also control the number of decimal places by using the maximumFractionDigits property:

const { format } = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 1, currencyDisplay: 'name', }); console.log(format(101.04)); // 101.0 US dollars console.log(format(99.99)); // 100.0 US dollars

If you need to round the fractional digits of the currency to the nearest fractional increment, for example 0.1, you can use the roundingIncrement property:

const { format } = new Intl.NumberFormat('en-Za', { style: 'currency', currency: 'ZAR', maximumFractionDigits: 2, roundingIncrement: 10, }); console.log(format(84.34)); // R 84,30 console.log(format(22.35)); // R 22,40

Get Started With Sentry

Get actionable, code-level insights to resolve JavaScript performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a JavaScript project and note your DSN

  3. Grab the Sentry JavaScript SDK

<script src="https://browser.sentry-cdn.com/7.112.2/bundle.min.js"></script>
  1. Configure your DSN
Sentry.init({ dsn: 'https://<key>@sentry.io/<project>' });

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.