const testPair = [
[0, ‘零‘],
[1, ‘一‘],
[2, ‘二‘],
[3, ‘三‘],
[10, ‘一十‘],
[11, ‘十一‘]
]
function transform (str) {
const numChar = {
‘零‘: 0,
‘一‘: 1,
‘二‘: 2,
‘三‘: 3,
‘四‘: 4,
‘五‘: 5,
‘六‘: 6,
‘七‘: 7,
‘八‘: 8,
‘九‘: 9
}
const levelChar = {
‘十‘: 10,
‘百‘: 100,
‘千‘: 1000,
‘万‘: 10000,
‘亿‘: 100000000
}
let ary = Array.from(str)
let temp = 0
let sum = 0
for (let i = 0; i < ary.length; i++) {
let char = ary[i]
if (char === ‘零‘) continue
if (char === ‘亿‘ || char === ‘万‘) {
sum += temp * levelChar[char]
temp = 0
} else {
let next = ary[i + 1]
if (next && next !== ‘亿‘ && next !== ‘万‘) {
temp += numChar[char] * levelChar[next]
i++
} else {
temp += numChar[char]
}
}
}
return sum + temp
}
console.log(transform(‘七万八千四百五十六‘))
原文:https://www.cnblogs.com/yaxinwang/p/13826166.html