js将人民币小写金额转换为大写

人民币大写在线转换工具

以下为es6写法,可以直接在控制栏粘贴运行

/**
 * 将人民币小写金额转换为大写
 * 
 * @class RMB
 * 示例
    let rmb = new RMB()
    console.log(rmb.transform(2114523.234))// 贰佰壹拾壹万肆仟伍佰贰拾叁元贰角叁分
 */
class RMB {
    constructor() {
        this.numMap = new Map([[0, '零'], [1, '壹'], [2, '贰'], [3, '叁'], [4, '肆'], [5, '伍'], [6, '陆'], [7, '柒'], [8, '捌'], [9, '玖']])
        this.integerMap = new Map([[0, '元'], [1, '拾'], [2, '佰'], [3, '仟'], [4, '万'], [5, '拾'], [6, '佰'], [7, '仟'], [8, '亿']])
        this.decimalMap = new Map([[0, '分'], [1, '角']])
        this.resultSet = new Set()
    }

    /**
     * 转换
     * 
     * @param {number} num 待转换的整数
     * @returns 
     * @memberof RMB
     */
    transform(num) {
        this.resultSet.clear()
        num = ('' + num).split('.')
        let integer = num[0],
            decimal = num[1] ? num[1].substr(0, 2) : []
        this.add(integer, this.integerMap).add(decimal, this.decimalMap)
        return [...this.resultSet].join('')
    }

    /**
     * 转换
     * 
     * @param {array} numType 
     * @param {map} mapType 
     * @returns 
     * @memberof RMB
     */
    add(numType, mapType) {
        let len = numType.length
        for (let i = 0; i < len; i++) {
            this.resultSet.add(this.numMap.get(+numType[i]) + (+numType[i] ? mapType.get(len - i - 1) : ''))
        }
        return this
    }
}
let rmb = new RMB()
console.log(rmb.transform(2114523.234))// 贰佰壹拾壹万肆仟伍佰贰拾叁元贰角叁分
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值