Created
May 18, 2010 12:18
-
-
Save toolmantim/404927 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Javascript version of Allan Odgaard's thousands separator regex | |
// http://blog.macromates.com/2007/recursion-in-regular-expressions/ | |
123456789.toString().replace(/(\d{1,3})(?=(\d{3})+(?!\d))/g, '$1,'); | |
// => "123,456,789" |
only problem: it's the wrong language! ;)
Pfft, ‘wrong’ language. Ruby is delicious. Mind you, JavaScript and C are my main languages nowadays, but I still think my Ruby solution is pretty sexy ;D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A WILD ONE-UP APPEARS:
(It’s shorter without the radix-point float support:
number.to_s.reverse.scan(/\d{1,3}/).join(',').reverse
)And now, homework: Make it automatically handle internationalization! :D