Example 1: Format Numbers as Currency String
// program to format numbers as currency string
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
formatter.format(2500);
Output
$2,500.00
In the above program, we have used the Intl.NumberFormat
object.
The Intl.NumberFormat
object enables language-sensitive number formatting.
Example 2: Format Numbers as Currency String Using concatenation
// program to format numbers as currency string
const number = 1234.5678;
const result = '$ ' + number.toFixed(2);
console.log(result);
Output
$ 1234.57
In the above example, the toFixed(2)
method is used to round up the number to two decimal values.
'$'
is added to the number to convert it into a currency string.
Example 3: Format Numbers as Currency String Using toLocaleString()
// program to format numbers as currency string
const result = (2500).toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
console.log(result);
Output
$2,500.00
The toLocaleString()
method returns a string with a language-sensitive representation of that number.
Example 4: Format Numbers as Currency String Using RegEx
// program to format numbers as currency string
const result = 1234.5678.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
console.warn('$ ' + result);
Output
$ 1,234.57
In the above example, the replace() method is used with the RegEx pattern to replace the number to currency string.
The toFixed(2)
method is used to round up the number to two decimal values.
Also Read: