let transferChineseNum = function (num) {
if (Number(num) === 0) {
return ''
} else {
let needTransferNum = String(Number(num))
console.log(needTransferNum)
let unit = [
'千',
'百',
'拾',
'万',
'千',
'百',
'拾',
'亿',
'千',
'百',
'拾',
'万',
'千',
'百',
'拾',
'元',
'角',
'分'
]
let chineseNum = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
let result = ''
let decimalPointInd = needTransferNum.indexOf('.')
if (decimalPointInd >= 0) {
needTransferNum =
needTransferNum.substring(0, decimalPointInd) +
needTransferNum.substr(decimalPointInd + 1, 2) // 若为小数,截取需要使用的unit单位
} else {
needTransferNum += '00'
}
unit = unit.splice(unit.length - needTransferNum.length)
for (let i = 0; i < needTransferNum.length; i++) {
let transferNumEle = needTransferNum.charAt(i)
let transferNumEleByChinese = chineseNum[transferNumEle]
result += transferNumEleByChinese + unit[i]
}
return result
.replace(/零(千|百|拾|角)/g, '零')
.replace(/(零)+/g, '零')
.replace(/零(万|亿|元)/g, '$1')
.replace(/(亿)万|壹(拾)/g, '$1$2')
.replace(/^元零?|零分/g, '')
.replace(/元$/g, '元整') // 替换掉数字里面的零字符,得到结果
}
}
console.log(transferChineseNum(12345678901234.10))
//23 2121 1100 0400.21
console