zoukankan      html  css  js  c++  java
  • js 浮点数计算

    0.1 + 0.2 == 0.3 ?

    01 + 0.2 = 0.30000000000000004

    1、四舍五入 (0.1+0.2).toFixed(1) = 0.3 , 对于计算金额这种严谨的问题,不推荐使用

    2、(0.1 * 10  + 0.2 * 10)/ 10 =  0.3

    //加法   
    
        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.div = 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);   
        }   
    }
    

      

  • 相关阅读:
    流光shader 和 流光+扭曲shader
    unity3d android动态更新dll
    Shader之溶解效果的几种实现方法
    我的第一个法线贴图
    windows 函数
    MFC 消息框
    C++ MFC棋牌类小游戏day1
    C++STL 预定义函数对象和函数适配器
    C++STL 函数对象和谓词
    C++STL 算法
  • 原文地址:https://www.cnblogs.com/daji/p/9582697.html
Copyright © 2011-2022 走看看