Skip to content
Advertisement

Add commas or spaces to group every three digits

I have a function to add commas to numbers:

JavaScript

Unfortunately, it doesn’t like decimals very well. Given the following usage examples, what is the best way to extend my function?

JavaScript

Presumably the easiest way is to first split on the decimal point (if there is one). Where best to go from there?

Advertisement

Answer

Just split into two parts with ‘.’ and format them individually.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement