Matthew C.
—You want to format a number as a currency string to display to a user. How do you do this?
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
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.