function formatNum(num) { num=(num+'').match(/^(?:s*)(-?d+)(.(d+))?(?:s*)$/); if(!num)return ''; var int=num[1].split(''); for(var i=int.length;i>0;i-=3) (i!==int.length)&&int.splice(i,0,','); num[2]&&int.push(num[2]); return int.join(""); }
formatNum(123124.1252564353) //"123,124.1252564353"
formatNum(123124) //"123,124"
formatNum('123225676245342341676435134.23') //"123,225,676,245,342,341,676,435,134.23"
formatNum(-23412412) //"-23,412,412"