首页 > 其他分享 >千分位分隔符

千分位分隔符

时间:2023-02-03 14:55:12浏览次数:33  
标签:const endStr res 千分 endIdx startStr 分隔符

const thousandth = (num = 123456789, fixed = 0) => {
    const strNum = num.toFixed(fixed)
    const [startStr, endStr] = strNum.split('.')
    let endIdx = startStr.length - 1
    let n = 0
    const res = []
    while(endIdx > -1){
        res.unshift(startStr[endIdx])
        n++
        if(n % 3 === 0 && endIdx !== 0){
            res.unshift(',')
        }
        endIdx--
    }
    return endStr ? `${res.join('')}.${endStr}` : res.join('')
}

  

标签:const,endStr,res,千分,endIdx,startStr,分隔符
From: https://www.cnblogs.com/zhenjianyu/p/17089259.html

相关文章