美文网首页
JS浮点数运算处理

JS浮点数运算处理

作者: 遇而记起 | 来源:发表于2018-09-04 10:10 被阅读0次

一. 问题描述

     最近在做一个项目,页面上会存在一些JS浮点数的运算,发现JS浮点数运算存在一些bug.譬如:

        0.1+0.2 == 0.30000000000000004

        0.1 + 0.7 == 0.7999999999999999

        7*0.8 == 5.6000000000000005

        5.6/7 == 0.7999999999999999

   二.解决方案   

   JS运算后都会有很小的误差. 不像.Net或者Java那样准确. 主要是JS重点不在运算上面,可是有时候项目一定要用到.想了一下大概有两种解决方案

     A 方案一: 

    运算结果保留2-3位小数位数. 前端界面一般用到的运算比较少。精度要求不会太高。 所以取2位小数位即可。

     B. 方案二:

           将小数位数转换为整数运算. 譬如:

            0.1+0.2 =》 (1+2)/10 == 0.3

            0.1 + 0.7 =》 (1+7)/10 == 0.8

            7*0.8 == (7*8)/10 == 5.6

            5.6/7 == (56/7)/10 == 0.1

       为了方便调用. 所以我们可以提取一个公共的方法出来.譬如下面的JSMath库,JSMath重写了加减乘除. 会先将参数转换为整数再运算JSMath(参数1).操作(参数2)

       参数1和参数2分别就是运算的第一个Number和第二个Number. 计算后通过Value属性获取值.

(function() {

    var JSMath = function() {

        return this;

    }

    JSMath.prototype.from = function(value) {

        // 支持JSMath参数传递主要是用于嵌套的调用

        if ((typeof(value) == 'object') && (value.value != undefined)) {

            this.value = value.value;

        } else {

            this.value = value;

        }

        return this;

    }

 // 加法

    JSMath.prototype.add = function(value) {

        var thisValueString = this.value.toString();

        var valueString = value.toString();

        var timesCount1 = 0;

        var timesCount2 = 0;

        if (thisValueString.indexOf('.') > 0) {

            timesCount1 = thisValueString.split('.')[1].length;

        }

        if (valueString.indexOf('.') > 0) {

            timesCount2 = valueString.split('.')[1].length;

        }

        var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;

        this.value = ((Math.pow(10, maxtimeCount) * this.value + Math.pow(10, maxtimeCount) * value)) / Math.pow(10, maxtimeCount);

        return this;

    }

 // 减法

    JSMath.prototype.sub = function(value) {

        var thisValueString = this.value.toString();

        var valueString = value.toString();

        var timesCount1 = 0;

        var timesCount2 = 0;

        if (thisValueString.indexOf('.') > 0) {

            timesCount1 = thisValueString.split('.')[1].length;

        }

        if (valueString.indexOf('.') > 0) {

            timesCount2 = valueString.split('.')[1].length;

        }

        var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;

        this.value = ((Math.pow(10, maxtimeCount) * this.value - Math.pow(10, maxtimeCount) * value)) / Math.pow(10, maxtimeCount);

        return this;

    }

    // 除法

    JSMath.prototype.div = function(value) {

        var thisValueString = this.value.toString();

        var valueString = value.toString();

        var timesCount1 = 0;

        var timesCount2 = 0;

        if (thisValueString.indexOf('.') > 0) {

            timesCount1 = thisValueString.split('.')[1].length;

        }

        if (valueString.indexOf('.') > 0) {

            timesCount2 = valueString.split('.')[1].length;

        }

        var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;

        this.value = ((Math.pow(10, maxtimeCount) * this.value) / (Math.pow(10, maxtimeCount) * value));

        return this;

    }

 // 乘法

    JSMath.prototype.times = function(value) {

        var thisValueString = this.value.toString();

        var valueString = value.toString();

        var timesCount1 = 0;

        var timesCount2 = 0;

        if (thisValueString.indexOf('.') > 0) {

            timesCount1 = thisValueString.split('.')[1].length;

        }

        if (valueString.indexOf('.') > 0) {

            timesCount2 = valueString.split('.')[1].length;

        }

        var maxtimeCount =  timesCount1 + timesCount2;

        this.value = (Math.pow(10, maxtimeCount) * this.value * Math.pow(10, maxtimeCount) * value) / Math.pow(10, maxtimeCount * 2);

        return this;

    }

    if (window.JSMath == undefined) {

        window.JSMath = function(value) {

            var result = new JSMath();

            result.from(value);

            return result;

        }

    }

})()

           B1.基本运算

0.1+0.2

=> JSMath(0.1).add(0.2).value == 0.3

7+0.8

=> JSMath(7).times(0.8).value == 5.6

5.6/7

=> JSMath(5.6).div(7).value = 0.8

        B2.多目运算

0.05 + 0.05 + 0.2

=> JSMath(JSMath(0.05).add(0.05)).add(0.2).value == 0.3

(5+0.6)/7

=> JSMath(JSMath(5).add(0.6)).div(7).value == 0.8

  三.小总结

    上面自己自己暂时知道的一些解决方案.不太清楚是否有开源的更可靠的三方库来解决这个问题,如果有的话,希望博友推荐一下。

            贴一下Stockoverflow 里面看到的一些解决方案:

 http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript

 http://stackoverflow.com/questions/3556789/javascript-math-error-inexact-floats

相关文章

  • JS浮点数运算处理

    一. 问题描述 最近在做一个项目,页面上会存在一些JS浮点数的运算,发现JS浮点数运算存在一些bug.譬如: 0...

  • js 浮点数运算处理

    js中在执行加减乘除运算时候会造成如下问题 下面通过特殊处理解决这一问题 测试

  • js浮点数运算问题---莫名出现多位小数

    问题:js浮点数运算问题---莫名出现多位小数 // 原因:这是由于在运算的时候先把浮点数转化成二进制后进行运算,...

  • Javascript 盲点

    1.javestript 中所有的数字都是浮点型 js在浮点数运算时会出现多位小数的不准确结果,浮点数运算的误差。...

  • js浮点数运算问题--莫名出现多位小数

    问题:js浮点数运算时,莫名出现多位小数 原因:这是由于在运算的时候先把浮点数转化成二进制后进行运算,但是有的小数...

  • javascript运算精度

    一个经典的浮点数运算0.1+0.2 ==0.30000000000000004 js在运算的时候会把10进制转化为...

  • 浮点数计算中的精度丢失

    1 js浮点数运算精度丢失 如果你用过js计算浮点数你肯定会遇到过下面这种情况:(我的小学白读了吗==。) (其...

  • js浮点数运算

    https://github.com/nefe/number-precision 阿里大佬封装好的浮点数运算直接拿去用

  • js 处理四则运算失去精度问题

    js 处理四则运算失去精度问题 计算精度,特别是浮点数计算。举个栗子 然后我寻求各种解决办法,其中用了lodash...

  • [Java] BigDecimal的使用

    BigDecimal BigDecimal位于java.math.BigDecimal包,用于处理浮点数高精度运算...

网友评论

      本文标题:JS浮点数运算处理

      本文链接:https://www.haomeiwen.com/subject/tntgwftx.html