Comma-formated numbers in jQuery
<p>I have following function in jQuery to format the number into comma-formatted:</p>

<pre><code>function CommaFormattedN(amount) {

    var delimiter = ","; 
    var i = parseInt(amount);

    if(isNaN(i)) { return ''; }

    i = Math.abs(i);

    var minus = '';
    if (i &lt; 0) { minus = '-'; }

    var n = new String(i);
    var a = [];

    while(n.length &gt; 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }

    if (n.length &gt; 0) { a.unshift(n); }

    n = a.join(delimiter);

    amount = minus + n;

    return amount;

}
</code></pre>

<p>I am calling this function like this on </p>

<pre><code> $('.text_field1').bind("focus blur change keyup", function(){    
 var $el = $(this);
    $el.val(CommaFormattedN($el.val()));
});
</code></pre>

<p>It's working fine, but the problem is, when number of digits increase from 5, it makes nothing.  All digits are deleted and it starts again.</p>
