实现加减乘除的插件

因为在做带小数的计算时,总会出现10.999999998这种类似的结果。

故有了这个插件。

直接上代码:

index.html

<body>
  <input type="text" id="number" placeholder="1,2,3">
  <br>
  <select name="" id="arith">
    <option value="0">加法</option>
    <option value="1">减法</option>
    <option value="2">乘法</option>
    <option value="3">除法</option>
  </select>
  <br>
  <button id="btn">计算</button>

  <div id="result"></div>

  <script>
  document.getElementById('btn').onclick = function(){
    var number = document.getElementById('number').value;
    var meth = document.getElementById('arith').value;
    var result = document.getElementById('result');
    if(number !== ''){
      var arr = number.split(',');
      switch (meth){
        case "0":
        try{
          result.innerHTML = arith.sum.apply(arith,arr);
        }catch(e){
          console.log(e.name+': '+ e.msg);
        }
        break;
        case "1":
        try{
          result.innerHTML = arith.exc.apply(arith,arr);
        }catch(e){
          console.log(e.name+': '+ e.msg);
        }
        break;
        case "2":
        try{
          result.innerHTML = arith.mul.apply(arith,arr);
        }catch(e){
          console.log(e.name+': '+ e.msg);
        }
        break;
        case "3":
        try{
          result.innerHTML = arith.div.apply(arith,arr);
        }catch(e){
          console.log(e.name+': '+ e.msg);
        }
        break;
        default:
        throw{
          name:'Error',
          msg:meth
        }
        break;
       }
     }else{
       result.innerHTML = '请输入计算数';
     }
  }
  </script>

</body>

arith.js

var arith={
  data:[],
  /**检查入参的数据类型 */
  _int: function(){
    var i,flag=true,arr=[],c=0;
    for(i=0;i<this.data.length;i++){
      var num = this.data[i].toString().replace(/\ +/g,'');
      num = num.replace(/,/g,'');
      if(isNaN(Number(num))){
        flag = false;
        return false;
      }
      arr.push(Number(num));
      var d;
      try{
        d = this.data[i].toString().split('.')[1].length;
      }catch(e){
        d = 0;
      }
      c = Math.max(c,d);
    }
    if(flag){
      arr.max = c;
      return arr;
    }else{
      return false;
    }
  },
/**加法 */
  sum: function(){
    this.data = arguments;
    if(this._int()){
      var data = this._int();
      var i,result=0;
        for(i=0;i<data.length;i++){
          var d;
          try{
            d = data[i].toString().split('.')[1].length;
          }catch(e){
            d = 0;
          }
          result += Number(data[i].toString().replace('.',''))*Math.pow(10,data.max - d);
        }
        return result/Math.pow(10,data.max);
      }else{
        throw{
          name:'Error',
          msg:'The arguments of function arith.sum is not Number'
        }
      }
  },
  /**减法 */
  exc: function(){
    this.data = arguments;
      if(this._int()){
        var data = this._int();
        var i,result = 'begain';
        for(i=0;i<data.length;i++){
          var d;
          try{
            d = data[i].toString().split('.')[1].length;
          }catch(e){
            d = 0;
          }
        if(result == 'begain'){
          result = Number(data[i].toString().replace('.',''))*Math.pow(10,data.max - d);
        }else{
          result += -Number(data[i].toString().replace('.',''))*Math.pow(10,data.max - d);
        }
      }
      return result/Math.pow(10,data.max);
      }else{
        throw{
          name:'Error',
          msg:'The arguments of function arith.exc is not Number'
        }
      }
    },
  /**乘法 */
  mul: function(){
    this.data = arguments;
    if(this._int()){
      var data = this._int();
      var i,result='begain',s=0;
      for(i=0;i<data.length;i++){
        var d;
        try{
          d = data[i].toString().split('.')[1].length;
        }catch(e){
          d = 0;
        }
        s += d;
        if(result == 'begain'){
          result = Number(data[i].toString().replace('.',''));
        }else{
          result = result*Number(data[i].toString().replace('.',''));
        }
      }
      return result/Math.pow(10,s);
      }else{
        throw{
          name:'Error',
          msg:'The arguments of function arith.mul is not Number'
        }
      }
    },
  /**除法 */
  div: function(){
    this.data = arguments;
    if(this._int()){
      var data = this._int();
      var i,result='begain',s=0,m=0;
      for(i=0;i<data.length;i++){
        var d;
        try{
          d = data[i].toString().split('.')[1].length;
        }catch(e){
          d = 0;
        }
        if(result == 'begain'){
          s = d;
          result = Number(data[i].toString().replace('.',''));
        }else{
          m += d;
          result = result/Number(data[i].toString().replace('.',''));
         }
        }
        return result*Math.pow(10,m-s);
        }else{
          throw{
            name:'Error',
            msg:'The arguments of function arith.div is not Number'
          }
        }
      }
    }

欢迎小伙伴们试用和提bug

posted @ 2018-08-31 15:44  徐念念  阅读(471)  评论(0编辑  收藏  举报