JavaScript Program to Format Numbers as Currency Strings

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Format Numbers as Currency String

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

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()

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

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.