js javascript 将中文数字转成阿拉伯数字

2021/4/9 11:12:00webjs

例: 一千一百万零一十一点零零一:1100011.001

function replaceTitle(chnStr) {
    var rtn = 0;
    var section = 0;
    var number = 0;
    var secUnit = false;
    var chnNumChar = {
        零: 0,
        一: 1,
        二: 2,
        三: 3,
        四: 4,
        五: 5,
        六: 6,
        七: 7,
        八: 8,
        九: 9
    };
    var chnNameValue = {
        十: { value: 10, secUnit: false },
        百: { value: 100, secUnit: false },
        千: { value: 1000, secUnit: false },
        万: { value: 10000, secUnit: true },
        亿: { value: 100000000, secUnit: true }
    }

    let numList = chnStr.split('点')
    var str = numList[0].split('');
    for (var i = 0; i < str.length; i++) {
        var num = chnNumChar[str[i]];
        if (typeof num !== 'undefined') {
            number = num;
            if (i === str.length - 1) {
                section += number;
            }
        } else {
            var unit = chnNameValue[str[i]].value;
            secUnit = chnNameValue[str[i]].secUnit;
            if (secUnit) {
                section = (section + number) * unit;
                rtn += section;
                section = 0;
            } else {
                section += (number * unit);
            }
            number = 0;
        }
    }
    if (numList[1]) {
        let dotNum = numList[1].split('').map(item => chnNumChar[item]).join('')
        return parseFloat(rtn + section + '.' + dotNum)
    }
    return rtn + section;
}