用 js 实现千位分隔符

1 function thousandBitSeparator(num) {
2   //return a && b means "return a if a is falsy, return b if a is truthy".
3     return num && (num.toString().indexOf('.') != -1 ? num.toString().replace(/(d)(?=(d{3})+.)/g, function($0, $1) {
4         return $1 + ",";
5     }) : num.toString().replace(/(d)(?=(d{3}))/g, function($0, $1) {
6         return $1 + ",";
7     }));
8 }

更多将输入的复杂字符串转为预期结果的方法,请参见 千位分隔符的完整攻略

原文地址:https://www.cnblogs.com/zhangchuanfeng/p/6018707.html