js收据、发票等表格数据格式化,js把数字转为中文大写

前端利用css做收据、发票,需格式化金额的格式,遍历出上面格式内容

1.表格金额格式化
/**
 * 把金额转为发票、收据等表格能遍历显示的数据
 * 
 * @param {money} Numner 金额大小
 * @param {max} Numner 带(角、分的位数)
 * 例:百	十	万	千	百	十	元	角	分为9位
 */
function moenyToStr(money, max = 9) {
    // NaN !== NaN
    if (Number(money) !== Number(money)) {
        return money
    }
    // 转为字符串
    let str = String(money)
    // 分割为数组,去掉小数点
    const parts = str.split('.')
    str = parts.join('')
    // 不足两位小数添0
    str = str.padEnd(parts[0].length + 2, '0')

    // 大于最大位数
    if (str.length > max) {
        return ''.padStart(max, '9')
        // return Array.from({length: max}, (x, i) => {
        //     return 9
        // })
    }
    // 不足位数补空
    str = str.padStart(max, ' ')
    return str
},
2.数字转为中文大写
function moneyToCN(money) {
    // NaN !== NaN
    if (Number(money) !== Number(money)) {
        return money
    }
    // 大数位,每四位一节
    const cnFigure = ['', '万', '亿', '万亿']
    // 中文数字0-9
    const cnInteger = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
    // 十进制计数“个十百千”
    const cnDecimalism = ['', '拾', '佰', '仟']
    // 先转为字符串
    const string = money + ''
    // 小数点分割
    const numbers = string.split('.')
    const integerPart = numbers[0]
    const decimalPart = numbers[1]
    // 整数部分位数
    const l = integerPart.length
    // 四位分节
    const pitch = Math.ceil(l / 4)
    const array = []
    // 分为每四位一节,从低位向高位分
    for (let index = 0; index < pitch; index++) {
        array.push(integerPart.slice((l - (index + 1) * 4 < 0) ? 0 : (l - (index + 1) * 4), l - index * 4))
    }
    // 将四位一节的数字转换为对应的中文,拼接上大数位,比如(一千一百一十一)(万),“一千一百一十一万”
    const cnArray = array.map((figure, index) => {
        const cn = figure.split('').map((char, i) => {
            return char === '0' ? '零' : (cnInteger[Number(char)] + cnDecimalism[figure.length - i - 1])
            // replace末尾的所有“零”,例如“壹仟零零零”变成“壹仟”;再replace中间的多个零,例如“壹万零零零玖”变成“壹万零玖”
        }).join('').replace(/\u96f6+$/, '').replace(/\u96f6+/g, '\u96f6')
        // 防止出现“零零零零”变成“”之后再加大数位,例如“(零零零零)万”变成“万”
        return cn.length ? (cn + cnFigure[index]) : (cnFigure[index] ? '零' : '')
    }).reverse()
    // 整数,替换掉中间连续的“零”,例如“一万亿零(亿)零(万)一千”变为“一万亿零一千”,最后可加上.replace(/^\u4e00\u5341/, '\u5341'),将“一十****”变为“十****”
    const integerCN = cnArray.join('').replace(/\u96f6+/g, '\u96f6')

    let decimalCN = ''
    if (!decimalPart || decimalPart === '0' || decimalPart === '00') {
        decimalCN = '整'
    } else {
        const [p0, p1] = decimalPart.split('')
        decimalCN = (p0 === '0' ? '零' : (cnInteger[Number(p0)] + '角')) + (!p1 || p1 === '0' ? '' : (cnInteger[Number(p1)] + '分'))
    }

    return `${integerCN || '零'}${decimalCN}`
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值