I'm trying to have my numbers in a format that is easier for the user to read. Eg. Instead of: 12349568483, it would be: 12 349 568 483. I was wondering if there is a simple way to do this?
Asked
Active
Viewed 1,401 times
-3
-
2Then it has to be a string, there are no spaces in numbers ? – adeneo Jul 11 '14 at 17:45
-
5`'2349568483'.replace(/(\d{3})/g, "$1 ")` -> http://jsfiddle.net/adeneo/7Nmng/ – adeneo Jul 11 '14 at 17:47
-
yea, it would need to be a string, I just didn't know what code to use to add spaces to the specific spots. Thanks! – GracieBee Jul 11 '14 at 17:48
-
See http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Nivas Jul 11 '14 at 17:48
1 Answers
1
var a = "12349568483";
a = a.split('').reverse().join('').replace(/([0-9]{3})/g, "$1 ").split('').reverse().join('');

Nikhil Baliga
- 1,339
- 12
- 18
-
The only reason this is more convoluted than the regular one is because it splits from the right rather than from left because they are numbers... ;) – Nikhil Baliga Jul 11 '14 at 17:51