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插件