关于0.1+0.2!=0.3浮点数解决方法


方法一:使用github上的库:BigDecimal.jsbignumber.js

 

方法二:使用简单点四舍五入方法,其实跟上面的方法差不多,只不过取了一个10位小数

function numTofixed(num) {
    if (typeof num == 'number') {
        num = parseFloat(num.toFixed(10))
    }
    return num;
}
numTofixed(0.1 + 0.2);

方法三:

function fixFloatCalcRudely(num){
    if(typeof num == 'number'){
        var str=num.toString(),
            match=str.match(/\.(\d*?)(9|0)\2{5,}(\d{1,5})$/);
        if(match != null){
            return num.toFixed(match[1].length)-0;
        }
    }
    return num;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM