function thousandSeparator(num) { return num && (num .toString().indexOf(‘.‘) != -1 ? num.toString().replace(/(\d)(?=(\d{3})+\.)/g, function($1, $2) { return $2 + ","; }) : num.toString().replace(/(\d)(?=(\d{3})+\b)/g, function($1, $2) { return $2 + ","; })); } console.log(thousandSeparator(1000000));//1,000,000 console.log(thousandSeparator(4656262.262));//4,656,262.262
根据freeyiyi1993的思路做了细微修改,原文有bug。
原文:http://www.cnblogs.com/zczhangcui/p/6361558.html