js 金额大写转换

在这里插入图片描述

methods: {
//this.toRmb(100)
//金额大写
	toRmb(val) {
		const amount = +val
		if (Number.isNaN(amount) || amount < 0) return ''
		const NUMBER = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
		const N_UNIT1 = ['', '拾', '佰', '仟']
		const N_UNIT2 = ['', '万', '亿', '兆']
		const D_UNIT = ['角', '分', '厘', '毫']
		let [integer, decimal] = amount.toString().split('.')
		if (integer && integer.length > 15) return '数字较大溢出'
		let res = ''
		// 整数部分
		if (integer) {
			for (let i = 0, len = integer.length; i < len; i++) {
				const num = integer.charAt(i)
				const pos = len - i - 1 // 排除个位后 所处的索引位置
				if (num === '0') { // 当前位 等于 0 且下一位也等于 0 则可跳过计算
					if (i === len - 1) {
						if (integer.length === 1) res += '零' // 0.35 这种情况不可跳过计算
						break
					}
					if (integer.charAt(i + 1) === '0') continue
				}
				res += NUMBER[num]
				if (parseInt(num)) res += N_UNIT1[(pos) % 4]
				if (pos % 4 === 0) res += N_UNIT2[Math.floor(pos / 4)]
			}
		}
		res += '元'
		// 小数部分
		if (parseInt(decimal)) {
			for (let i = 0; i < 4; i++) {
				const num = decimal.charAt(i)
				if (parseInt(num)) res += NUMBER[num] + D_UNIT[i]
			}
		} else {
			res += '整'
		}
		return res
	}
	}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值