How to print a number with commas as thousands separators in JavaScript?
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> |
chevron_right
filter_none
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> |
chevron_right
filter_none
Output:
- Before clicking the button:
- After clicking the button:
Recommended Posts:
- JavaScript | Split a string with multiple separators
- How to print JavaScript console with PHP ?
- How to create a pop-up to print dialog box using JavaScript?
- How to print the content of an object in JavaScript ?
- Print the content of a div element using JavaScript
- JavaScript | Window print() Method
- How to pretty print JSON string in JavaScript ?
- Print current day and time using HTML and JavaScript
- How to print debug messages in the Google Chrome JavaScript Console?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- JavaScript | Number.MAX_VALUE & Number.MIN_VALUE with Examples
- How to convert a float number to the whole number in JavaScript?
- Number.isNaN() In JavaScript
- Factorial of a number using JavaScript
- JavaScript | Number isSafeInteger()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



