将数字金额转化为大写金额,且数字金额最多只保留两位小数

采用element ui 中的input框实现,填写小写金额,自动实现大写金额

截图:

 

 

代码:

数字转大写金额实现:

export function dealBigMoney(n) {
  var fraction = ['角', '分'];
  var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  var unit = [
    ['元', '万', '亿'],
    ['', '拾', '佰', '仟']
  ];
  var head = n < 0 ? '欠' : '';
  n = Math.abs(n);

  var s = '';

  for (var i = 0; i < fraction.length; i++) {
    s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
  }
  s = s || '整';
  n = Math.floor(n);

  for (var i = 0; i < unit[0].length && n > 0; i++) {
    var p = '';
    for (var j = 0; j < unit[1].length && n > 0; j++) {
      p = digit[n % 10] + unit[1][j] + p;
      n = Math.floor(n / 10);
    }
    s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
  }
  return (
    head +
    s
      .replace(/(零.)*零元/, '元')
      .replace(/(零.)+/g, '零')
      .replace(/^整$/, '零元整')
  );
}

限制数字以及小数点保留两位:

BigMoney(param, num) {
  if (num) {
     this.contentObj[param] =('' + num) // 第一步:转成字符串
          .replace(/[^\d^\.]+/g, '') // 第二步:把不是数字,不是小数点的过滤掉
          .replace(/^0+(\d)/, '$1') // 第三步:第一位0开头,0后面为数字,则过滤掉,取后面的数字
          .replace(/^\./, '0.') // 第四步:如果输入的第一位为小数点,则替换成 0. 实现自动补全
          .match(/^\d*(\.?\d{0,2})/g)[0] || ''; // 第五步:最终匹配得到结果 以数字开头,只有一个小数点,而且小数点后面只能有0到2位小数
     this.$set(this.contentObj,'reimbursementAmountUppercase',dealBigMoney(this.contentObj[param]));
  }
},

input框:

<div class="contentInput" v-else-if="stampObj.includes('Lowercase')">
  报销金额(小写) :
   <el-input
       type="number"
       :autosize="{ minRows: 1, maxRows: 2 }"
        placeholder="请输入内容"
        size="mini"
        :disabled="disabledData"
        v-model="contentObj[stampObj]"
        @change="BigMoney(stampObj, contentObj[stampObj])"
    ></el-input>
</div>
<div class="contentInput" v-else-if="stampObj.includes('Uppercase')">
   报销金额(大写) :
   <el-input
        type="textarea"
        :autosize="{ minRows: 1, maxRows: 2 }"
        placeholder="请输入内容"
        size="mini"
        :disabled="true"
         v-model="contentObj.reimbursementAmountUppercase"
    ></el-input>
</div>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是将数字金额换为中文大写金额JavaScript代码: ```javascript function convertToChineseNumeral(amount) { const chineseNumeral = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; const chineseUnit = ["", "拾", "佰", "仟"]; const chineseBigUnit = ["", "萬", "亿", "兆"]; let num = parseFloat(amount); let chineseAmount = ""; if (isNaN(num)) { return ""; } let isNegative = num < 0; if (isNegative) { num = -num; } let fraction = Math.round((num - Math.floor(num)) * 100); if (fraction == 0) { chineseAmount += "整"; } else { chineseAmount += chineseNumeral[Math.floor(fraction / 10)] + "角"; chineseAmount += chineseNumeral[fraction % 10] + "分"; } num = Math.floor(num); let unitIndex = 0; while (num > 0) { let currentUnit = ""; for (let i = 0; i < 4 && num > 0; i++) { let currentNumeral = num % 10; if (currentNumeral > 0) { currentUnit = chineseUnit[i] + currentUnit; currentUnit = chineseNumeral[currentNumeral] + currentUnit; } else if (currentUnit != "") { currentUnit = chineseNumeral[currentNumeral] + currentUnit; } num = Math.floor(num / 10); } if (currentUnit != "") { currentUnit += chineseBigUnit[unitIndex]; } chineseAmount = currentUnit + chineseAmount; unitIndex++; } if (chineseAmount == "") { chineseAmount = chineseNumeral[0]; } if (isNegative) { chineseAmount = "负" + chineseAmount; } return chineseAmount; } console.log(convertToChineseNumeral(123456789.01)); // 壹亿贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元零壹分 ``` 希望这个代码能帮到你。如果你有其它问题,欢迎继续询问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值