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('.'); | |
}; |
Thanks for posting. It'd be great if rails shipped with javascript versions of most of the view helpers...
Thank you for sharing! This is exactly what I want.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit goes to http://kevinvaldek.com/number-with-delimiter-in-javascript
I just swiped that extended Number.prototype with it (and he swiped it from Rails, were I originally went to swipe from).