我对 JavaScript 很陌生,试图理解 JavaScript 中的一些数学逻辑,知道为什么 if 条件中的代码没有执行吗?
index.js
var a = 0.1,
b = 0.2,
c = Math.random(a + b);
if(c === 0.3) {
console.log('fun');
}
最佳答案
Math.random()
给出 0 到 1 之间的随机数。
使用Math.round()
对 float 进行四舍五入,从而消除 float 差异。
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 - 为什么我的 if 条件里面的表达式不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36360356/