金额转换成大写和简称的方法

/**
   * 获取金额的中文大写或简称的方法
   * @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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值