首页 > 代码库 > numberToCapital() JavaScript数字金额转换为大写金额

numberToCapital() JavaScript数字金额转换为大写金额

function numberToCapital(number) {    if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(number)) {        return "数字格式不正确!"    }    // 如果有小数,截取整数位和小数位    number += ‘00‘;    var pointIndex = number.indexOf(‘.‘);    if (pointIndex > -1) {        number = number.substring(0, pointIndex) + number.substr(pointIndex + 1, 2);    }    var unit = ‘千百十亿千百十万千百十元角分‘;    var covertStr = ‘‘;    // 根据输入金额的长度匹配对应的单位    unit = unit.substr(unit.length - number.length);    for (var i = 0; i < number.length; i++) {        covertStr += ‘零壹贰叁肆伍陆柒捌玖‘.charAt(number.charAt(i)) + unit.charAt(i);    }    return covertStr.replace(/零(千|百|拾|角)/g, ‘零‘)        .replace(/(零)+/g, ‘零‘)        .replace(/零(万|亿|元)/g, ‘$1‘)        .replace(/(亿)万|元(拾)/g, ‘$1$2‘).replace(/^元零?|零分/g, ‘‘)        .replace(/元$/g, ‘元整‘);}console.log(numberConvert(21324873.234));//"贰千壹百叁十贰万肆千捌百柒十叁元贰角叁分"

 

numberToCapital() JavaScript数字金额转换为大写金额