Created
February 11, 2011 04:10
-
-
Save scottwb/821904 to your computer and use it in GitHub Desktop.
Rails-like number_with_delimiter in javascript
This file contains 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
Number.prototype.number_with_delimiter = function(delimiter) { | |
var number = this + '', delimiter = delimiter || ','; | |
var split = number.split('.'); | |
split[0] = split[0].replace( | |
/(\d)(?=(\d\d\d)+(?!\d))/g, | |
'$1' + delimiter | |
); | |
return split.join('.'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for sharing! This is exactly what I want.