千分位分隔符
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 thousandth = (num = 123456789, fixed = 2) => { if (typeof num !== 'number') return new Error('请传入数字') const [startStr, endStr] = num.toFixed(fixed).split('.') const res = [] for (let i = startStr.length; i > -1; i -= 3) { if (i > 3) { res.unshift(',', ...startStr.slice(i - 3, i)) } else { res.unshift(...startStr.slice(0, i)) } } return endStr ? `${res.join('')}.${endStr}` : res.join('') }
以自己现在的努力程度,还没有资格和别人拼天赋