我是JavaScript的新手,试图了解JavaScript中的一些数学逻辑,知道为什么如果条件未执行,我的代码为何?
index.js
var a = 0.1,
b = 0.2,
c = Math.random(a + b);
if(c === 0.3) {
console.log('fun');
}
最佳答案
Math.random()
给出一个介于零和一之间的随机数。
使用Math.round()
舍入浮点数,从而消除浮点数差。
var a = 0.1,
b = 0.2,
c = Math.round((a + b) * 100) / 100; // Round numbers to single decimal point
if (c === 0.3) {
关于javascript - 如果条件不起作用,为什么我的表达式在里面?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36360356/