JS解决加减乘除浮点类型丢失精度问题

更新日期: 2020-02-24阅读: 1.5k标签: 运算

当我们在前端使用js来执行运算时,会有丢失精度的问题。

例如:

console.log("使用js原生态方法");
console.log(" 1.01 + 1.02 ="+(1.01 + 1.02));
console.log(" 1.01 - 1.02 ="+(1.01 - 1.02));
console.log(" 0.000001 / 0.0001 ="+(0.000001 / 0.0001));
console.log(" 0.012345 * 0.000001 ="+(0.012345 * 0.000001));
    
-----------------------------------
输出结果:
使用js原生态方法
1.01 + 1.02 =2.0300000000000002
1.01 - 1.02 =-0.010000000000000009
0.000001 / 0.0001 =0.009999999999999998
0.012345 * 0.000001 =1.2344999999999999e-8


解决方法:

<script type="text/javascript">
    // 两个浮点数求和
    function accAdd(num1,num2){
       var r1,r2,m;
       try{
           r1 = num1.toString().split('.')[1].length;
       }catch(e){
           r1 = 0;
       }
       try{
           r2=num2.toString().split(".")[1].length;
       }catch(e){
           r2=0;
       }
       m=Math.pow(10,Math.max(r1,r2));
       // return (num1*m+num2*m)/m;
       return Math.round(num1*m+num2*m)/m;
    }
    
    // 两个浮点数相减
    function accSub(num1,num2){
       var r1,r2,m;
       try{
           r1 = num1.toString().split('.')[1].length;
       }catch(e){
           r1 = 0;
       }
       try{
           r2=num2.toString().split(".")[1].length;
       }catch(e){
           r2=0;
       }
       m=Math.pow(10,Math.max(r1,r2));
       n=(r1>=r2)?r1:r2;
       return (Math.round(num1*m-num2*m)/m).toFixed(n);
    }
 
    // 两个浮点数相除
    function accDiv(num1,num2){
       var t1,t2,r1,r2;
       try{
           t1 = num1.toString().split('.')[1].length;
       }catch(e){
           t1 = 0;
       }
       try{
           t2=num2.toString().split(".")[1].length;
       }catch(e){
           t2=0;
       }
       r1=Number(num1.toString().replace(".",""));
       r2=Number(num2.toString().replace(".",""));
       return (r1/r2)*Math.pow(10,t2-t1);
    }
    
       // 两个浮点数相乘
    function accMul(num1,num2){
       var m=0,s1=num1.toString(),s2=num2.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);
    }
    
  </script>
  
  <script>
console.log("使用js原生态方法");
    console.log(" 1.01 + 1.02 ="+(1.01 + 1.02));
    console.log(" 1.01 - 1.02 ="+(1.01 - 1.02));
    console.log(" 0.000001 / 0.0001 ="+(0.000001 / 0.0001));
    console.log(" 0.012345 * 0.000001 ="+(0.012345 * 0.000001));
    console.log("-----------------");
    console.log("使用自定义方法");
    console.log(" 1.01 + 1.02 ="+accAdd(1.01,1.02));
    console.log(" 1.01 - 1.02 ="+accSub(1.01,1.02));
    console.log(" 0.000001 / 0.0001 ="+accDiv(0.000001,0.0001));
    console.log(" 0.012345 * 0.000001 ="+accMul(0.012345,0.000001)); 
  </script> 



------------------
输出结果

使用js原生态方法
1.01 + 1.02 =2.0300000000000002
1.01 - 1.02 =-0.010000000000000009
0.000001 / 0.0001 =0.009999999999999998
0.012345 * 0.000001 =1.2344999999999999e-8
-----------------
使用自定义方法:
1.01 + 1.02 =2.03
1.01 - 1.02 =-0.01
0.000001 / 0.0001 =0.01
0.012345 * 0.000001 =1.2345e-8

链接: https://www.fly63.com/article/detial/9153

js除了Math.floor方法,还可以通过位运算|,>>实现向下取整

我们都知道通过Math.floor()方法可实现数值的向下取整,得到小于或等于该数字的最大整数。除了Math.floor方法,还可以使用位运算|,>>来实现向下取整哦

es6 扩展运算符 三个点(...)

扩展运算符( spread )是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。

巧用JS位运算

位运算的方法在其它语言也是一样的,不局限于JS,所以本文提到的位运算也适用于其它语言。位运算是低级的运算操作,所以速度往往也是最快的

js中使用位运算,让执行效率更高

平常的数值运算,其本质都是先转换成二进制再进行运算的,而位运算是直接进行二进制运算,所以原则上位运算的执行效率是比较高的,由于位运算的博大精深,下面通过一些在js中使用位运算的实例

js各种取整方式及方法_四舍五入、向上取整、向下取整

js实现:四舍五入、向上取整、向下取整等方法。parseInt、Math.ceil、Math.round、Math.floor、toFixed等的使用

JavaScript循环计数器

JS经常会遇到延迟执行的动作,并且失败后自动尝试,尝试N次之后就不再尝试的需求,今天刚好又遇到,于是写个闭包,以后不断完善继续复用。检查并计数第一个参数用来标记是尝试哪个动作的,第二个参数是最大尝试次数

js 位运算符_js按位运算符及其妙用

大多数语言都提供了按位运算符,恰当的使用按位运算符有时候会取得的很好的效果。在我看来按位运算符应该有7个:& 按位与、| 按位或、^ 按位异或、~ 按位非

PHP取整、四舍五入取整、向上取整、向下取整、小数截取

PHP取整数函数常用的四种方法:1.直接取整,舍弃小数,保留整数:intval(); 2.四舍五入取整:round(); 3.向上取整,有小数就加1:ceil(); 4.向下取整:floor()。

JavaScript 中的相等操作符 ( 详解 [] == []、[] == ![]、{} == !{} )

ECMAScript 中的相等操作符由两个等于号 ( == ) 表示,如果两个操作数相等,则返回 true。相等操作符会先转换操作数(通常称为强制转型),然后比较它们的相等性。

关于js开发中保留小数位计算函数(以向上取整或向下取整的方式保留小数)

前端工作中经常遇到数字计算保留小数问题,由于不是四舍五入的方式不能使用toFixed函数,本文采用正则表达式匹配字符串的方式,解决对数字的向上或向下保留小数问题:

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!