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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 25-Jan-2018 16:24:43
Advertisements