const toDecimal2 = (x) => { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x * 100) / 100; var s = f.toString(); var rs = s.indexOf('.'); if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + 2) { s += '0'; } return s;};const FenToYuan = (fen) => { var num = fen; num = fen * 0.01; num += ''; var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g; num = num.replace(reg, '$1'); num = toDecimal2(num); return num;};for (let i = 10000; i < 100000; i++) { const n = FenToYuan(i); console.log(n); if (n.length > 9) { console.log(n.length); }}原文:https://juejin.cn/post/7099650508591202341