Adding commas to numbers

adding commas to numbers

by Frederick Jaime

JavaScript

function numberWithCommas(x) {
    x = x.toString();
    var pattern = /(-?\d+)(\d{3})/;
    while (pattern.test(x))
        x = x.replace(pattern, "$1,$2");
    return x;
}

alert(numberWithCommas(10001000000));