<分区>
如果我做 0.3 - 0.2 它给出 0.9999999998 而不是 0.1 有没有办法让它给出一个精确的小数?如果计算器不精确,就不能相信它。
标签 javascript
<分区>
如果我做 0.3 - 0.2 它给出 0.9999999998 而不是 0.1 有没有办法让它给出一个精确的小数?如果计算器不精确,就不能相信它。
最佳答案
您可以尝试这个解决方案:https://30secondsofinterviews.org/#what-does-0-1-0-2-0-3-evaluate-to-
A solution to this problem would be to use a function that determines if two numbers are approximately equal by defining an error margin (epsilon) value that the difference between two values should be less than.
const approxEqual = (n1, n2, epsilon = 0.0001) => Math.abs(n1 - n2) < epsilon approxEqual(0.1 + 0.2, 0.3) // true
关于javascript - 小数点不精确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51977668/