// 有无小数部分function separate(num) {if (typeof num !== "number") {throw new TypeError('Type Error')}let pre, n = 0;if (num.toString().includes('.')) {pre = num.toString().split('.')[0]} else {pre = num.toString()}let a = num.toString().split('')for (let i = pre.length; i > 0; i--) {n++;if (!(n % 3)) {a.splice(i - 1, 0, ',')}}let r = a.join('').charAt(0)return r === ',' ? a.join('').substr(1) : a.join('')}console.log(separate(1212312339.23)); // 1,212,312,339.23console.log(separate(121231233923)); // 121,231,233,923
