**注意处理小数点的情况: **

function splitByDot(num = '') {
    let temp = num.split('.');
    let toDo;
    if (temp.length === 2) {
        toDo = temp[0]
    } else {
        toDo = temp;
    }
    toDo = toDo.toString();
    let s = '';
    for (let index = toDo.length -1; index >= 0; index--) {
        s = toDo.charAt(index) + s;
        if (index % 3 === 0 && index !== 0) {
            s = ',' + s
        }
    }
    return temp[1] ? s + '.' +temp[1].toString() :s;
}
console.log(a('123456.1023')) // 123,456.1023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
最后更新时间: 6/20/2022, 10:48:50 PM