1. // 有无小数部分
    2. function separate(num) {
    3. if (typeof num !== "number") {
    4. throw new TypeError('Type Error')
    5. }
    6. let pre, n = 0;
    7. if (num.toString().includes('.')) {
    8. pre = num.toString().split('.')[0]
    9. } else {
    10. pre = num.toString()
    11. }
    12. let a = num.toString().split('')
    13. for (let i = pre.length; i > 0; i--) {
    14. n++;
    15. if (!(n % 3)) {
    16. a.splice(i - 1, 0, ',')
    17. }
    18. }
    19. let r = a.join('').charAt(0)
    20. return r === ',' ? a.join('').substr(1) : a.join('')
    21. }
    22. console.log(separate(1212312339.23)); // 1,212,312,339.23
    23. console.log(separate(121231233923)); // 121,231,233,923