标签:money indexof log chinese int class math 转换 ber
/** * @desc 金额转换成大写 * @param {Number} n 需要转换成大写的金额 * @return {String} 转换后的大写中文金额 * @example convertIntegerToChineseCash(‘123‘) =>‘壹百贰拾叁元整‘ */ function convertIntegerToChineseCash(n) { let fraction = [‘角‘, ‘分‘] let digit = [‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘, ‘柒‘, ‘捌‘, ‘玖‘] let unit = [ [‘元‘, ‘万‘, ‘亿‘, ‘万‘], [‘‘, ‘拾‘, ‘佰‘, ‘仟‘] ] n = Math.abs(n) n = moneyFormat01(n, 2).replace(/,/g, ‘‘); //!!!需要结合金额格式化函数,也可以注释掉本行 console.log("+++++++", n) let s = ‘‘ let k = ‘‘ if (String(n).indexOf(‘.‘) == -1) { n = n + ‘.00‘ k = n.split(‘.‘)[1].split(‘‘) } else { k = String(n).split(‘.‘)[1].split(‘‘) } // for(let i = 0; i < k.length; i++){ // s += (digit[Math.floor(Math.pow(10, i)*10*n) % 10] + fraction[i]).replace(/零./, ‘‘) // } for (let i = 0; i < k.length; i++) { s += (digit[k[i]] + fraction[i]).replace(/零./, ‘‘) } s = s || ‘整‘ n = Math.floor(n) for (let i = 0; i < unit[0].length && n > 0; i++) { let p = ‘‘ for (let j = 0; j < unit[1].length && n > 0; j++) { p = digit[n % 10] + unit[1][j] + p n = Math.floor(n / 10) } s = p.replace(/(零.)*零$/, ‘‘).replace(/^$/, ‘零‘) + unit[0][i] + s } return s.replace(/(零.)*零元/, ‘元‘).replace(/(零.)+/g, ‘零‘).replace(/^整$/, ‘零元整‘) } console.log(convertIntegerToChineseCash(120))
标签:money indexof log chinese int class math 转换 ber
原文地址:https://www.cnblogs.com/qinyuanchun/p/13378448.html