Return to Snippet

Revision: 51108
at September 14, 2011 19:49 by fengelz


Initial Code
formatNumber = function (number) {
        number = String(number).replace(new RegExp(/\s/g), '');
        var length = number.length;
        var x = number.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
        }
        return x1 + x2;
    }

Initial URL

                                

Initial Description
Thanks to http://www.mredkj.com/javascript/numberFormat.html#addcommas

Initial Title
format thousands

Initial Tags
number, format

Initial Language
JavaScript