
1.表格金额格式化
function moenyToStr(money, max = 9) {
if (Number(money) !== Number(money)) {
return money
}
let str = String(money)
const parts = str.split('.')
str = parts.join('')
str = str.padEnd(parts[0].length + 2, '0')
if (str.length > max) {
return ''.padStart(max, '9')
}
str = str.padStart(max, ' ')
return str
},
2.数字转为中文大写
function moneyToCN(money) {
if (Number(money) !== Number(money)) {
return money
}
const cnFigure = ['', '万', '亿', '万亿']
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])
}).join('').replace(/\u96f6+$/, '').replace(/\u96f6+/g, '\u96f6')
return cn.length ? (cn + cnFigure[index]) : (cnFigure[index] ? '零' : '')
}).reverse()
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}`
}