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
}
}
js 金额大写转换
最新推荐文章于 2024-07-24 16:20:17 发布