Vue中阿拉伯数字与汉字的相互转换

阿拉伯数字转汉字:

    numberfilter (num) {
      const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] // changeNum[0] = "零"
      const unit = ['', '十', '百']
      num = parseInt(num)
      console.log(num);
      const getWan = (temp) => {
        const strArr = temp.toString().split('').reverse()
        console.log(strArr);
        let newNum = ''
        for (var i = 0; i < strArr.length; i++) {
          newNum = (i == 0 && strArr[i] == 0 ? '' : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? '' : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum
        }
        return newNum
      }
      const overWan = Math.floor(num / 100)
      console.log(overWan);
      let noWan = num % 100
      console.log(noWan);
      if (noWan.toString().length < 2) noWan = '0' + noWan
      let strr = (overWan ? getWan(overWan) + '百' + getWan(noWan) : getWan(num))
      console.log(strr.split('')[0]);
      if(strr.split('')[0] == '一'){
        return strr.substring(1)
      }else{
        return overWan ? getWan(overWan) + '百' + getWan(noWan) : getWan(num)
      }
    },

可以搭配filter使用:

汉字转阿拉伯数字

data:

      chnNumChar : {:0,:1,:2,:3,:4,:5,:6,:7,:8,:9
      },
      chnNameValue : {:{value:10, secUnit:false},:{value:100, secUnit:false},:{value:1000, secUnit:false},:{value:10000, secUnit:true},
        亿:{value:100000000, secUnit:true}
      }

methods:

ChineseToNumber(chnStr) {
      var rtn = 0;
      var section = 0;
      var number = 0;
      var secUnit = false;
      var str = chnStr.split("");

      for (var i = 0; i < str.length; i++) {
        var num = this.chnNumChar[str[i]];
        if (typeof num !== "undefined") {
          number = num;
          if (i === str.length - 1) {
            section += number;
          }
        } else {
          var unit = this.chnNameValue[str[i]].value;
          secUnit = this.chnNameValue[str[i]].secUnit;
          if (secUnit) {
            section = (section + number) * unit;
            rtn += section;
            section = 0;
          } else {
            section += number * unit;
          }
          number = 0;
        }
      }
      return rtn + section;
    },
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 护眼 设计师:闪电赇 返回首页