LearningRecord icon indicating copy to clipboard operation
LearningRecord copied to clipboard

格式化金钱,每千分位加逗号

Open Rashomon511 opened this issue 5 years ago • 0 comments

function format(str) {
    let s = ''
    let count = 0
    for (let i = str.length - 1; i >= 0; i--) {
        s = str[i] + s
        count++
        if (count % 3 == 0 && i != 0) {
            s = ',' + s
        }
    }
    return s
}
function format(str) {
    return str.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
}

Rashomon511 avatar Jun 22 '19 09:06 Rashomon511