SOURCE


function toThousands(num) {
    const str = num.toString();
    let result = '';
    let counter = 0;

    for (var i = str.length - 1; i >= 0; i--) {
        counter++;
        result = str.charAt(i) + result;

        // 能整除3,但不是0 % 3,则加逗号
        if (!(counter % 3) && i !== 0) { 
            result = ',' + result; 
        }
    }

    return result;
}

console.log(toThousands(112345678)); //'112,345,678'
console.log(toThousands(12345678)); //'12,345,678'
console 命令行工具 X clear

                    
>
console