Javascript display numbers with commas

function commaSeparated(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }
    return val;
}

// Call Function
console.log(commaSeparated(1)); // 1
console.log(commaSeparated(10)); // 10
console.log(commaSeparated(100)); // 100
console.log(commaSeparated(1000)); // 1,000
console.log(commaSeparated(10000)); //10,000

Comments

Popular posts from this blog

HTML5 video loop src change on end play function

Set limit wp_nav_menu

Calculate percentage of a specific element has been scrolled into visible on the screen