VUE + VUX 客户端 金额,输入数字同步翻译成大小汉字金额

3 篇文章 0 订阅
3 篇文章 0 订阅

is-required  这个组件的源码 https://blog.csdn.net/dyp330/article/details/85781622  

<template>
  <div>
    <!--金额,输入数字同步翻译成大小汉字金额-->
    <div class="mt-10">
      <span class="amountTitle">
     {{title}}
        <!---是否必填,显示红*-->
     <is-required :isRequired="isRequired"></is-required>
      </span>
      <div style="width: 80%; float: left; padding-right: 5px;">
        <input v-model="price" type="number" class="numberClass"
               @keyup="onKeyup"/>
      </div>
    </div>
    <label style="color: #959595; padding-left: 15px;font-size: 14px;">大写</label>
    <span class="chineseClass">{{simplifiedChinese}}</span>
  </div>
</template>

<script>
  import {TransferDom} from 'vux';
  import IsRequired from '@/components/IsRequired';
  
  export default {
    name: 'Amount',
    props: {
      pholder: {
        type: String,
        default: '请输入'
      },
      title: {
        type: String,
        default: '金额'
      },
      isRequired: {
        type: Boolean,
        default: true,
      }
    },
    components: {
      IsRequired
    },
    directives: {
      TransferDom,
    },
    data() {
      return {
        errorShow: false,
        errorText: '',
        price: null,
        simplifiedChinese: '',
      };
    },
    methods: {
      onKeyup() {
        if (this.price) {
          if (this.price.length <= 10) {
            this.simplifiedChinese = this.smalltoBIG(this.price);
          } else {
            this.simplifiedChinese = '非法输入';
          }
        } else {
          this.simplifiedChinese = '';
        }
      },
      smalltoBIG(price) {
        let fraction = ['角', '分'];
        let digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
        let unit = [['圆', '万', '亿'], ['', '拾', '佰', '仟']];
        let head = price < 0 ? '欠' : '';
        price = Math.abs(price);
        let upper = '';
        for (let i = 0; i < fraction.length; i++) {
          upper += (digit[Math.floor(price * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        upper = upper || '整';
        price = Math.floor(price);
        for (let i = 0; i < unit[0].length && price > 0; i++) {
          let p = '';
          for (let j = 0; j < unit[1].length && price > 0; j++) {
            p = digit[price % 10] + unit[1][j] + p;
            price = Math.floor(price / 10);
          }
          upper = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + upper;
        }
        return head + upper.replace(/(零.)*零圆/, '圆').replace(/(零.)+/g, '零').replace(/^整$/, '零圆整');
      },
    },
  };
</script>
<style type="less">

  .chineseClass {
    font-size: 14px;
    color: #959595;
    text-align: right;
    display: inline-block;
    width: 80%;
  }

  .numberClass {
    text-align: right;
    border: 0;
    width: 100%;
    outline: 0;
    -webkit-appearance: none;
    background-color: transparent;
    height: 1.41176471em;
    line-height: 1.41176471;
    padding-right: 20px;
  }
  .amountTitle {
    padding-left: 15px;
    display: inline-block;
    width: 20%;
    float: left;
  }
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值