← #57 Find scrollable elements

Format a number as a currency string

Topics: JavaScript

Given a number, we can format it as a currency string without using an external libary.

The NumberFormat API provides the easy way to format a currency of a given country:

const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,

The optional minimumFractionDigits parameter indicates the minium number of digits in the fraction part. Calling the format function will format the input, and prefix or suffix the currency depending on the country.

formatter.format(2345);         // '$2,345.00'
formatter.format('2345'); // '$2,345.00'
formatter.format('10000000'); // '$10,000,000.00'
Fix a typo or suggest an improvement
#59 Pass an array as function arguments