前景提要
1234567890 -> '1,234,567,890'
P.S. 考虑兼容性和性能问题 我写的代码: function numToStr(num) { // STEP 1: type conversion let tempStr = String(num) // STEP 2: modular -> 3 let m = tempStr.length % 3 // STEP 3: slice -> '1 | 234567890' let part1 = `${tempStr.slice(0, m)},` let tempPart2 = tempStr.slice(m), part2 = '' // STEP 4: split -> '234,567,890' for (let i = 0; i < tempPart2.length; i++) { if (i > 0 && i % 3 === 0) { part2 += ',' } part2 += tempPart2.charAt(i) } // STEP 5: combine -> '1,234,567,890' let str = part1 + part2 return str } const number = 1234567890 numToStr(number) // '1,234,567,890' 专业程序员的解法 function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }