Skip to content

JS format currency | Code

  • by

Use toLocaleString method or Intl.NumberFormat to format currency-based locality in JS. The toLocaleString() method returns a string with a language-sensitive representation of that number.

JS format currency

Simple example code print currency format in JavaScript.

<!DOCTYPE html>
<html>
<body>
  <script>
    function formatMoney(number) {
      return number.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
    }

    console.log(formatMoney(10000));
    console.log(formatMoney(1000000));

    // OR
    var n = 1000;
    console.log(n.toLocaleString('en-IN', { style: 'currency', currency: 'INR' }));

  </script>
</body>
</html>

Output:

JS format currency

Or you can custom Using concatenation.

function formatMoney(number) {
   return '$ '+ number.toLocaleString('en-US');
}

Format Numbers as Currency String, The Intl.NumberFormat object enables language-sensitive number formatting.

// program to format numbers as currency string
const formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD'
});

formatter.format(2500); // $2,500.00

Do comment if you have any doubts or suggestions on this JS code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading