This question already has answers here:
Is floating point math broken?
                            
                                (33个答案)
                            
                    
                3年前关闭。
        

    

当我尝试计算一个简单的浮点值时,我得到了错误的结果:

var a = 0.1111;
var b = 1 * 100;
alert(b); // Returns 11.110000000000001


Is floating point math broken?讨论了为什么会发生这种情况,但是如何在javacript中解决此问题呢?

https://jsfiddle.net/Lc0805mt/1/

最佳答案

这可以帮助您:



<html>
<head>
</head>
    <body>
       <script>
           var a = 0.1111;
           var b = ((a*10000)*100)/10000;
           alert(b);
        </script>
    </body>
</html>

关于javascript - Javascript计算浮点值错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37985643/

10-09 14:49