Format number with comma - Node.js Number

Node.js examples for Number:Format

Description

Format number with comma

Demo Code


/**/*from   ww w .  j a v  a2s.co  m*/
* Format number with comma
* @type {string|number} x
*/
function addCommas(x) {
        if (isNaN(x)) {
            return '-';
        }
        x = (x + '').split('.');
        return x[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g,'$1,')
               + (x.length > 1 ? ('.' + x[1]) : '');
}

Related Tutorials