Skip to content

Instantly share code, notes, and snippets.

@scottwb
Created February 11, 2011 04:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottwb/821904 to your computer and use it in GitHub Desktop.
Save scottwb/821904 to your computer and use it in GitHub Desktop.
Rails-like number_with_delimiter in javascript
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('.');
};
@scottwb
Copy link
Author

scottwb commented Feb 11, 2011

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).

@barelyknown
Copy link

Thanks for posting. It'd be great if rails shipped with javascript versions of most of the view helpers...

@sergio1990
Copy link

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