/**
* 获取金额的中文大写或简称的方法
* @author xxx
* @param money 传入的金额字符串
* @param simple 为true代表金额简称,false为繁体字大写,默认为false
*/
const getMoneyUpper = function (money, simple = false) {
if (!(Object.prototype.toString.call(money) == '[object Number]' || Object.prototype.toString.call(money) == '[object String]')) return money;
money = money.toString();
const arr = money.split('.');
let dict = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
let unit = ['', '十', '百', '千', '万', '亿', '元'];
if (!simple) {
dict = ['', '壹', '貳', '叁', '肆', '伍', '陸', '柒', '捌', '玖'];
unit = ['', '拾', '佰', '仟', '万', '亿', '元'];
}
function getL(m) {
m = m.split('').reverse().join('');
let str = '';
let tmp = '';
for (let i = 0; i < m.length; i++) {
if (m[i] != 0) {
tmp = dict[m[i]] + unit[i % 4] + tmp;
} else {
if (str && str[0] != '零') {
tmp = '零';
}
}
if (i < 4) {
str = tmp + str;
tmp = '';
}
if (tmp && (i == 7 || (i < 7 && i == m.length - 1))) {
tmp += '万';
str = tmp + str;
tmp = '';
}
if (tmp && ((i > 7 && i == m.length - 1))) {
tmp += '亿';
str = tmp + str;
tmp = '';
}
}
if (str) {
str += '元';
}
str = str.replace('零万', '万');
if (str.endsWith('零')) {
str = str.substr(0, str.length - 1);
}
if (!str) {
str = '零';
}
return str;
}
function getR(m) {
if (m == undefined || m.length < 0) {
return '整';
}
let str = '';
if (m[0] && m[0] != '0') {
str = str + dict[m[0]] + '角';
}
if (m[1] && m[1] != '0') {
str = str + dict[m[1]] + '分';
}
if (str == '') {
str = '整';
}
return str;
}
let result = getL(arr[0]) + getR(arr[1]);
(result === '整') && (result = '');
(result.startsWith('零')) && (result = result.substring(1));
return result;
}
金额转换成大写和简称的方法
最新推荐文章于 2024-10-18 11:12:30 发布