方法一:使用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;
}

 

相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2021-08-10
  • 2021-07-21
  • 2021-11-07
  • 2021-12-10
  • 2022-12-23
猜你喜欢
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2021-09-03
  • 2022-12-23
  • 2021-09-28
相关资源
相似解决方案