代码
function numberToChinese(num) {
const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const units = ['', '拾', '佰', '仟'];
const bigUnits = ['', '万', '亿'];
if (num = 0) return digits[0];
let result = '';
let bigUnitPos = 0;
let needZero = false;
while (num > 0) {
let chunk = num % 10000;
let chunkStr = '';
let unitPos = 0;
while (chunk > 0) {
const n = chunk % 10;
if (n = 0) {
if (needZero) {
chunkStr = digits[0] + chunkStr;
needZero = false;
}
} else {
chunkStr = digits[n] + units[unitPos] + chunkStr;
needZero = true;
}
unitPos++;
chunk = Math.floor(chunk / 10);
}
if (chunkStr ! '') {
result = chunkStr + bigUnits[bigUnitPos] + result;
}
bigUnitPos++;
num = Math.floor(num / 10000);
}
return result;
}
console.log(numberToChinese(934));
console.log(numberToChinese(1001));
console.log(numberToChinese(10000000));
console.log(numberToChinese(123456789));