我有一个函数,它接受一个随机浮点数作为参数。函数中的if条件检查浮点数是否等于或大于特定点数,例如0.002
另一个恒定的浮点数x。下面是我创建的函数。
function checkRandom({ value }) {
const x = 0.98334
const diff = value - x
if (value == x || (value > x) diff) {
console.log("Match Successful")
} else {
console.log("")
}
}
checkRandom(0.97)
也许你是这个意思?
通过{value}
function checkRandom({value}) {
const x = 0.98334
const diff = value - x;
console.log(diff)
if (value == x || Math.abs(diff) < .002) {
console.log("Match Successful")
} else {
console.log("Difference is greater than .002")
}
}
checkRandom({value:0.98332})
只是通过一个数字
function checkRandom(num) {
const x = 0.98334
const diff = num - x;
console.log(diff)
if (num == x || Math.abs(diff) < .002) {
console.log("Match Successful")
} else {
console.log("Difference is greater than .002")
}
}
checkRandom(0.98332);