如何确定乘以最接近目标数字的数字?

How to determine the number that when multiplied will get you the closest to a target number?

假设我有数字 2062,乘数是 0.75

什么是 JavaScript 公式来计算哪个数乘以 0.75 最接近 2062?

PS:这里最接近的意思是等于(==)或者非常接近,但是低于目标数,不是很接近但是以上.

您正在 x * 0.75 = 2062 寻找 x。因此求解 x 应该是 x = 2062 / 0.75。为确保数字是小于或等于 x 的最接近整数,可以使用 Math.floor:

Math.floor(2062 / 0.75) = 2749
function findFactor(a, b) {
    return Math.floor(a / parseFloat(b));
}

findFactor(2062, 0.75) -> 2749

https://jsfiddle.net/0s8cr5gd/