获得了1-9乃至更大,但是我们需要使用一 - 九,那怎么办呢?
直接上代码吧:
getChineseNum(section) {
const chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
const chnUnitChar = ['', '十', '百', '千', '万', '亿', '万亿', '亿亿'];
let strIns = '', chnStr = '';
let unitPos = 0;
let zero = true;
while (section > 0) {
const v = section % 10;
if (v === 0) {
if (!zero) {
zero = true;
chnStr = chnNumChar[v] + chnStr;
}
} else {
zero = false;
strIns = chnNumChar[v];
strIns += chnUnitChar[unitPos];
chnStr = strIns + chnStr;
}
unitPos++;
section = Math.floor(section / 10);
}
return chnStr;
}
调用: getChineseNum(12);