Hide options

Format numbers with commas

This function formats a number with commas every 3 places. As example, a number like 1500000 will be returned as 1,500,000. You can define the thousands and decimal separator.
// This function adds a comma/point every 3 places.
// You can define the thousands and decimal separater.

function format_number(number) {

    var thousands = ',';
    var decimal = '.';
    var x = /(d+)(d{3})/;
    number += '';
    n = number.split('.');
    n1 = n[0];
    n2 = n.length > 1 ? decimal + n[1] : '';
    while (x.test(n1)) {
        n1 = n1.replace(x, '$1' + sep + '$2');
    }
    return n1 + n2;
}            



Top