Intl removed from latest build, another way to format currency?

I was using the Intl to format a number as a currency, and from what I can tell moment only deals with dates and times. Is there another way I can easily take a numeric value like ‘1000.5’ and convert it ‘$1,000.50’?

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

Hey @critchie1 ,

Welcome to the community :wave:

You can use the code below.

const formatToCurrency = amount => {
return "$" + amount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, "$&,");
};
console.log(formatToCurrency(1000.5));  

Let’s go one by one, the toFixed method is used to determine the number of decimal places of the number given as a parameter. The number of digits to be shown in fractions is determined by the number.

Regular expression for currency format is what I use with replace method. So that the values like below can be separated properly.

2 Likes

Awesome, I’ll give it a try.

Edit: Worked perfectly, and I was easily able to switch out the formatter function for the formatToCurrency.

1 Like