Computer >> Computer tutorials >  >> Programming >> Javascript

How can I format numbers as dollars currency string in JavaScript?


To format numbers as dollar current string, use number formatter, which is part of the Internationalization API.

Example

You can try to run the following code to format numbers as dollars currency string

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <p>
         Formatting 150 as US dollars (US$):
      </p>
      <script>
         var formatter = new Intl.NumberFormat('en-US', {
            style: 'currency',
            currency: 'USD',
            minimumFractionDigits: 2,
         });
         // for 150$
         document.write(formatter.format(150));
      </script>
   </body>
</html>