1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| const toChineseNum = (num) => { if (num === 0) { return '零'; } const bit = ['', '十', '百', '千']; const unit = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; const section = ['', '万']; let sectionPos = 0; let resultStr = ''; let piece, needZero = false; function toCNum(n) { let bitPos = 0, rs = '', zeroNum = 0; while (n > 0) { let curNum = n % 10; if (curNum === 0) { zeroNum++; bitPos++; } else { if (bitPos === zeroNum) zeroNum = 0; else if (zeroNum > 0) { rs = '零' + rs; zeroNum = 0; } rs = unit[curNum] + bit[bitPos++] + rs; } n = Math.floor(n / 10); } return rs; } while (num > 0) { piece = num % 10000; resultStr = toCNum(piece) + section[sectionPos] + (needZero ? '零' : '') + resultStr; needZero = piece < 1000 && piece > 0; sectionPos++; num = Math.floor(num / 10000); } return resultStr; } 或 const toChineseNum = (num) => { let str = '零一二三四五六七八九', str2 = ['', '十', '百', '千']; let Clu = Array.from( [...num.toString()].reverse(), (v, i) => str[v] + (i == 4 ? '万' : (v == 0 ? '' : str2[i % 4]))).reverse().join('') .replace(/零+/g, '零') .replace(/(零+$)/, '') .replace(/零+(?=万)/, ''); return Clu; } 或 const toChineseNum = (num) => { let unit = ['', '十', '百', '千', '万'] let numArr = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] let arrNum = num.toString().split("").reverse(); let strArr = arrNum.map((v, i) => { if (i === 4 && parseInt(v) === 0) { return "万" } i = i > 4 ? i - 4 : i; return parseInt(v) === 0 ? numArr[v] : numArr[v] + unit[i - 0] }) return strArr.reverse().join("").replace(/(零(?=零))|(零$)|(零(?=万))/g, '') }
|