美文网首页
阿拉伯数字转成大写中文的函数

阿拉伯数字转成大写中文的函数

作者: 前端技师胡帅博 | 来源:发表于2019-08-21 17:33 被阅读0次
convertCurrency(val) {
            let money = val;
            const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
            const cnIntRadice = ['', '拾', '佰', '仟'];
            const cnIntUnits = ['', '万', '亿', '兆'];
            const cnDecUnits = ['角', '分', '毫', '厘'];
            const cnInteger = '整';
            const cnIntLast = '元';
            const maxNum = 999999999999999.9999;
            let integerNum = '';
            let decimalNum;
            let chineseStr = '';
            let parts;
            if (money === '') { return ''; }
            money = parseFloat(money);
            if (money >= maxNum) {
                return '';
            }
            if (money === 0) {
                chineseStr = cnNums[0] + cnIntLast + cnInteger;
                return chineseStr;
            }
            money = money.toString();
            if (money.indexOf('.') === -1) {
                integerNum = money;
                decimalNum = '';
            } else {
                parts = money.split('.');
                integerNum = parts[0];
                decimalNum = parts[1].substr(0, 4);
            }
            if (parseInt(integerNum, 10) > 0) {
                let zeroCount = 0;
                const IntLen = integerNum.length;
                for (let i = 0; i < IntLen; i++) {
                    const n = integerNum.substr(i, 1);
                    const p = IntLen - i - 1;
                    const q = p / 4;
                    const m = p % 4;
                    if (n === '0') {
                        zeroCount++;
                    } else {
                        if (zeroCount > 0) {
                            chineseStr += cnNums[0];
                        }
                        zeroCount = 0;
                        chineseStr += cnNums[parseInt(n, 10)] + cnIntRadice[m];
                    }
                    if (m === 0 && zeroCount < 4) {
                        chineseStr += cnIntUnits[q];
                    }
                }
                chineseStr += cnIntLast;
            }
            if (decimalNum !== '') {
                const decLen = decimalNum.length;
                for (let i = 0; i < decLen; i++) {
                    const n = decimalNum.substr(i, 1);
                    if (n !== '0') {
                        chineseStr += cnNums[Number(n)] + cnDecUnits[i];
                    }
                }
            }
            if (chineseStr === '') {
                chineseStr += cnNums[0] + cnIntLast + cnInteger;
            } else if (decimalNum === '') {
                chineseStr += cnInteger;
            }
            return chineseStr;
        }

相关文章

网友评论

      本文标题:阿拉伯数字转成大写中文的函数

      本文链接:https://www.haomeiwen.com/subject/chmisctx.html