js 阿拉伯数字转成中文数字
时间: 2023-11-12 13:07:13 浏览: 100
以下是将阿拉伯数字转换为中文数字的 JavaScript 代码:
```javascript
function convertNumberToChinese(num) {
const chineseNumArr = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
const chineseUnitArr = ["", "十", "百", "千", "万", "亿"];
let chineseNum = "";
let unitPos = 0;
let needZero = false;
if (num === 0) {
return chineseNumArr[0];
}
while (num > 0) {
const digit = num % 10;
if (digit === 0) {
if (needZero) {
chineseNum = chineseNumArr[0] + chineseNum;
}
needZero = true;
} else {
needZero = false;
chineseNum = chineseNumArr[digit] + chineseUnitArr[unitPos] + chineseNum;
}
unitPos++;
num = Math.floor(num / 10);
}
return chineseNum;
}
```
示例:
```javascript
console.log(convertNumberToChinese(1234)); // 一千二百三十四
console.log(convertNumberToChinese(1204)); // 一千二百零四
console.log(convertNumberToChinese(10000)); // 一万
console.log(convertNumberToChinese(100000000)); // 一亿
```
注意:该代码只支持转换非负整数。
阅读全文