// val是金额可以是数字/字符串 len是小数点后几位,默认2位
const formatUpper = (val, len = 2) => {
let n = val.toString();
const flagLen = typeof len !== 'number' || (typeof len === 'number' && (len < 0 || len > 6));
if (!val) {
return;
}
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n) || flagLen) {
return '数据非法';
}
let unit: string = '千百拾亿千百拾万千百拾元角分厘毫微纳皮';
let str: string = '';
const p: number = n.indexOf('.');
n += '0'.repeat(len);
const unitFrac: number = unit.slice(unit.indexOf('元') + 1).length;
if (p >= 0) {
n = n.substring(0, p) + n.substr(p + 1, len);
unit = unit.substr(len - unitFrac - n.length, n.length);
} else {
unit = unit.substr(len - unitFrac - n.length, n.length + len);
}
for (var i = 0; i < n.length; i++) {
str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
}
return str
.replace(/零(千|百|拾)/g, '零')
.replace(/(零)+/g, '零')
.replace(/零(万|亿|元)/g, '$1')
.replace(/(亿)万|壹(拾)/g, '$1$2')
.replace(/^元零?|零角|零分|零厘|零毫|零微|零纳|零皮/g, '')
.replace(/元$/g, '元整');
};