Thursday, February 10, 2011

Rails-like number_with_delimiter in javascript

The number_with_delimiter view helper is Rails is nice. I wanted to do the same thing in Javascript. I went to just grab it from Rails and port it to js. But first I googled and found someone had already done exactly that. I took his code and made it extend Number.protoype so you can do something like:


var num = 12345678;
var str = num.number_with_delimiter();
alert(str);


This will show that str is "12,345,678".

Her's my version of the code:



Based on code from: http://kevinvaldek.com/number-with-delimiter-in-javascript