<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <title>Document</title> </head> <body> <script> function num2Chinese(num) { if (num === '0') { return '零' } const numArr = num.toString().replace(/(?=(\d{4})+(?!\d))/g, ',').split(',').filter(Boolean) let str = '' const binaryUnit = ['', '万', '亿'] const numUnit = ['', '十', '百', '千'] const toLower = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] numArr.map((n, idx) => { // 去掉0000, 比如说 一亿零万零三百 => 一亿零三百 if (n !== '0000') { let temp = '' n.split('').map((ui, uidx) => { const uUnit = numUnit[n.length - uidx - 1] temp += `${toLower[parseInt(ui)]}${uUnit}` }) // 去掉结尾的零 str += temp.replace(/(?<=.*?)零+$/g, '') const unit = binaryUnit[numArr.length - idx - 1] str += unit } }) str = str .replace(/((零百)|(零千)|(零十))+/g, '零') // 去掉十百千前面的零 .replace(/(?<=.*?)零+$/g, '') // 去掉结尾的零 .replace(/(零+万)/g, '万') // 去掉万前面的零 .replace(/(零+亿)/g, '亿') // 去掉亿前面的零 return str } // 枚举测试 const num = ['102000304500', '3', '0', '3004', '30004', '1000000003', '10000200030'] num.forEach(i => { const result = num2Chinese(i) console.log(`${i} => ${result}`) }) // 102000304500 => 壹千零贰十亿零叁十万肆千伍百 // 3 => 叁 // 3004 => 叁千零肆 // 30004 => 叁万零肆 // 1000000003 => 壹十亿零叁 // 10000200030 => 壹百亿零贰十万零叁十 for (let i = 0; i <= 100; i++) { const result = num2Chinese(i) console.log(`${i} => ${result}`) } // 1 => 壹 // index.html:64 2 => 贰 // index.html:64 3 => 叁 // index.html:64 4 => 肆 // index.html:64 5 => 伍 // index.html:64 6 => 陆 // index.html:64 7 => 柒 // index.html:64 8 => 捌 // index.html:64 9 => 玖 // index.html:64 10 => 壹十 // index.html:64 11 => 壹十壹 // index.html:64 12 => 壹十贰 // index.html:64 13 => 壹十叁 // index.html:64 14 => 壹十肆 // index.html:64 15 => 壹十伍 // index.html:64 16 => 壹十陆 // index.html:64 17 => 壹十柒 // index.html:64 18 => 壹十捌 // index.html:64 19 => 壹十玖 // index.html:64 20 => 贰十 // index.html:64 21 => 贰十壹 // index.html:64 22 => 贰十贰 // index.html:64 23 => 贰十叁 // index.html:64 24 => 贰十肆 // index.html:64 25 => 贰十伍 // index.html:64 26 => 贰十陆 // index.html:64 27 => 贰十柒 // index.html:64 28 => 贰十捌 // index.html:64 29 => 贰十玖 // index.html:64 30 => 叁十 // index.html:64 31 => 叁十壹 // index.html:64 32 => 叁十贰 // index.html:64 33 => 叁十叁 // index.html:64 34 => 叁十肆 // index.html:64 35 => 叁十伍 // index.html:64 36 => 叁十陆 // index.html:64 37 => 叁十柒 // index.html:64 38 => 叁十捌 // index.html:64 39 => 叁十玖 // index.html:64 40 => 肆十 // index.html:64 41 => 肆十壹 // index.html:64 42 => 肆十贰 // index.html:64 43 => 肆十叁 // index.html:64 44 => 肆十肆 // index.html:64 45 => 肆十伍 // index.html:64 46 => 肆十陆 // index.html:64 47 => 肆十柒 // index.html:64 48 => 肆十捌 // index.html:64 49 => 肆十玖 // index.html:64 50 => 伍十 // index.html:64 51 => 伍十壹 // index.html:64 52 => 伍十贰 // index.html:64 53 => 伍十叁 // index.html:64 54 => 伍十肆 // index.html:64 55 => 伍十伍 // index.html:64 56 => 伍十陆 // index.html:64 57 => 伍十柒 // index.html:64 58 => 伍十捌 // index.html:64 59 => 伍十玖 // index.html:64 60 => 陆十 // index.html:64 61 => 陆十壹 // index.html:64 62 => 陆十贰 // index.html:64 63 => 陆十叁 // index.html:64 64 => 陆十肆 // index.html:64 65 => 陆十伍 // index.html:64 66 => 陆十陆 // index.html:64 67 => 陆十柒 // index.html:64 68 => 陆十捌 // index.html:64 69 => 陆十玖 // index.html:64 70 => 柒十 // index.html:64 71 => 柒十壹 // index.html:64 72 => 柒十贰 // index.html:64 73 => 柒十叁 // index.html:64 74 => 柒十肆 // index.html:64 75 => 柒十伍 // index.html:64 76 => 柒十陆 // index.html:64 77 => 柒十柒 // index.html:64 78 => 柒十捌 // index.html:64 79 => 柒十玖 // index.html:64 80 => 捌十 // index.html:64 81 => 捌十壹 // index.html:64 82 => 捌十贰 // index.html:64 83 => 捌十叁 // index.html:64 84 => 捌十肆 // index.html:64 85 => 捌十伍 // index.html:64 86 => 捌十陆 // index.html:64 87 => 捌十柒 // index.html:64 88 => 捌十捌 // index.html:64 89 => 捌十玖 // index.html:64 90 => 玖十 // index.html:64 91 => 玖十壹 // index.html:64 92 => 玖十贰 // index.html:64 93 => 玖十叁 // index.html:64 94 => 玖十肆 // index.html:64 95 => 玖十伍 // index.html:64 96 => 玖十陆 // index.html:64 97 => 玖十柒 // index.html:64 98 => 玖十捌 // index.html:64 99 => 玖十玖 // index.html:64 100 => 壹百 </script> </body> </html>
核心函数
function num2Chinese(num) { if (num === '0') { return '零' } const numArr = num.toString().replace(/(?=(\d{4})+(?!\d))/g, ',').split(',').filter(Boolean) let str = '' const binaryUnit = ['', '万', '亿'] const numUnit = ['', '十', '百', '千'] const toLower = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] numArr.map((n, idx) => { // 去掉0000, 比如说 一亿零万零三百 => 一亿零三百 if (n !== '0000') { let temp = '' n.split('').map((ui, uidx) => { const uUnit = numUnit[n.length - uidx - 1] temp += `${toLower[parseInt(ui)]}${uUnit}` }) // 去掉结尾的零 str += temp.replace(/(?<=.*?)零+$/g, '') const unit = binaryUnit[numArr.length - idx - 1] str += unit } }) str = str .replace(/((零百)|(零千)|(零十))+/g, '零') // 去掉十百千前面的零 .replace(/(?<=.*?)零+$/g, '') // 去掉结尾的零 .replace(/(零+万)/g, '万') // 去掉万前面的零 .replace(/(零+亿)/g, '亿') // 去掉亿前面的零 return str }
标签:index,中文,12,const,temp,num,js,html,64 From: https://www.cnblogs.com/fmg0224/p/17974305