文章正文

网站-修复javascript精度

【文章】2020-04-23

简介网站-修复javascript精度

<script type="text/javascript" >

   //加法     

    Number.prototype.add = function(arg){     

        var r1,r2,m;     

        try{r1=this.toString().split(".")[1].length}catch(e){r1=0}     

        try{r2=arg.toString().split(".")[1].length}catch(e){r2=0}     

        m=Math.pow(10,Math.max(r1,r2))     

        return (this*m+arg*m)/m     

    }    

    //减法     

     Number.prototype.sub = function (arg){     

         return this.add(-arg);     

     }     

     //乘法     

     Number.prototype.mul = function (arg)     

     {     

         var m=0,s1=this.toString(),s2=arg.toString();     

         try{m+=s1.split(".")[1].length}catch(e){}     

         try{m+=s2.split(".")[1].length}catch(e){}     

         return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)     

     }     

     //除法     

     Number.prototype.p = function (arg){     

         var t1=0,t2=0,r1,r2;     

         try{t1=this.toString().split(".")[1].length}catch(e){}     

         try{t2=arg.toString().split(".")[1].length}catch(e){}     

         with(Math){     

             r1=Number(this.toString().replace(".",""))     

             r2=Number(arg.toString().replace(".",""))     

             return (r1/r2)*pow(10,t2-t1);     

         }     

     }  

</script>

用法:

 var total = new Number(0.1).mul(0.56);

打赏支持

感谢您的支持,加油!

打开微信扫码打赏,你说多少就多少

找书费时,联系客服快速获取!

扫码支持

在线客服8:30-22:30,若离线请留言!

获取教程,请联系在线客服!

扫码支持

在线客服8:30-22:30,若离线请留言!

热门阅读

找PDF电子书,太费时间?

  • 微信扫描二维码,让客服快速查找。
  • 在线客服8:30-22:00,若离线请留言!