js小数精度问题

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        
        <script src="https://unpkg.com/mathjs@9.4.4/lib/browser/math.js"></script>
        
    </head>
    <body>
    </body>
</html>
<script>
    var num = 0;
    var arr = [0.01,0.02,0.03,0.04,0.5];
    for(var i=0;i<arr.length;i++){
        num = math.add(math.bignumber(arr[i]), math.bignumber(num)).toString(); //
    }
    console.log(num) //
    console.log(math.subtract(math.bignumber(900000000000), math.bignumber(9.2)).toString())  //
    console.log(math.multiply(math.bignumber(9), math.bignumber(9.12)).toString()) //
    console.log(math.divide(math.bignumber(9), math.bignumber(3)).toString()) //

</script>

使用math.js插件

posted on 2022-04-01 11:14  Web引领者  阅读(33)  评论(0编辑  收藏  举报