数字转汉字大写

该文章展示了一个JavaScript函数,用于将货币数值转换成中文大写的表示,包括元、角、分等单位,以及处理负数和零的情况。函数通过循环和数组映射实现数字到中文字符的转换,并进行相应的零和单位的优化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

const numberToCNchar = money => {
    const ArabicNumeralCapital = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" ];
    const AfterInt = ["角", "分"];
    const IntUnitSplit = ["元", "万", "亿", "兆"];
    const BeforeInt = ["", "拾", "佰", "仟"];
    const symbol = money < 0 ? "负" : "";
    money = Math.abs(money);
    let res = "";
    for(let i = 0; i < AfterInt.length; i++) {
        let num = Math.pow(10, i+1);
        num = Math.floor(num * money) % 10;
        res += (ArabicNumeralCapital[num] + AfterInt[i]).replace(/(零.)+/, '');
    }
    if (res.length < 1) {
        res = "整";
    }
    
    let IntPart = Math.floor(money);
    for(let i = 0; i < IntUnitSplit.length && IntPart > 0; i++) {
        let part = "";
        for(let j = 0; j < BeforeInt.length; j++) {
            let a = IntPart % 10;
            IntPart = Math.floor(IntPart / 10);
            part = ArabicNumeralCapital[a] + BeforeInt[j] + part;
        }
        res = part + IntUnitSplit[i] + res;
    }
    res=res.replace(/(零[拾佰仟])*零元/,"元");
    res=res.replace(/^(零.)+/,"");
    res=res.replace(/(零[拾佰仟])+/g,"零");
    res=res.replace(/零([万亿兆])+/g,"$1");
    res=res.replace(/零([万亿兆])+/g,"");
    res=res.replace(/^整$/, "零元整");
    return symbol + res;
}

const money = numberToCNchar('43043242.93');

console.log('money',money)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值