How to print a number with commas as thousands separators in JavaScript? - GeeksforGeeks

18 Oct, 2019
Method 1: Using Intl.NumberFormat()
The Intl.NumberFormat() object is used to represent numbers in a language-sensitive formatting. It can be used to represent currency or percentages according to the locale specified.
The locales parameter of this object is used to specify the format of the number. The ‘en-US’ locale is used to specify that the locale takes the format of the United States and the English language, where numbers are represented with a comma between the thousands.
The format() method of this object can be used to return a string of the number in the specified locale and formatting options. This will format the number with commas at the thousands of places and return a string with the formatted number.
Syntax:
nfObject = new Intl.NumberFormat('en-US')
nfObject.format(givenNumber)

Example:
<!DOCTYPE html><html>  <head>    <title>        Print a number with commas as      thousands separators in JavaScript.    </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>       How to print a number with commas as thousands       separators in JavaScript?    </b>    <p>Output for '123456789': <span class="output"></span>  </p>      <button onclick="separateNumber()">Separate thousands</button>    <script type="text/javascript">        function separateNumber() {            givenNumber = 123456789;              nfObject = new Intl.NumberFormat('en-US');            output = nfObject.format(givenNumber);              document.querySelector('.output').textContent = output;          }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
Method 2: Using toLocaleString()
The toLocaleString() method is used to return a string with a language-sensitive representation of a number. The optional locales parameter is used to specify the format of the number.
The locale ‘en-US’ is used to specify that the locale takes the format of the United States and the English language, where numbers are represented with a comma between the thousands. This will format the number with commas at the thousands of places and return a string with the formatted number.
Syntax:
givenNumber.toLocaleString('en-US')
Example:
<!DOCTYPE html><html>  <head>    <title>        Print a number with commas as       thousands separators in JavaScript.    </title></head>  <body>    <h1 style="color: green">GeeksforGeeks</h1>    <b>        How to print a number with commas as thousands        separators in JavaScript?    </b>    <p>Output for '987654321': <span class="output"></span>  </p>      <button onclick="separateNumber()">      Separate thousands  </button>    <script type="text/javascript">        function separateNumber() {            givenNumber = 987654321;              output = givenNumber.toLocaleString('en-US');              document.querySelector('.output').textContent = output;          }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?