Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display large numbers with commas? HTML

Tags:

I am wanting to display large numbers more nicely with commas. So if the number was say 123456789, it would display 123,456,789. I have looked around but I only found code that just wouldn't work the way I wanted so I was hoping I could find some help here. Also, I hope it is also dynamic, so the commas will change as the number changes.

The number that I want to affect has the id="value".

That should be all, I don't think I am missing anything. So again I want the number with an id="value" to have commas introduced when it's needed. If you need any more information please let me know!

like image 255
Cosmicluck Avatar asked Jan 04 '15 01:01

Cosmicluck


People also ask

How do I display large numbers with commas in HTML?

There's a simpler syntax for toLocaleString: Number(x). toLocaleString();

How can number be printed with commas separating the thousands?

You can use the toLocaleString() method to comma-separate thousands in a number for a given localization, such as 'en-US'. You can also use the Intl. NumberFormatter object to format a number to a specific localization. Lastly, you can also build your own custom function to get the job done.

How do you put a comma in input type numbers?

You can use type="text" and then add commas to the number in the onchange event.


1 Answers

You can use toLocaleString:

num.toLocaleString('en', {useGrouping:true}) 
like image 144
Oriol Avatar answered Oct 12 '22 17:10

Oriol