如何确定乘以后将使您最接近目标数字的数字

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

本文关键字:数字 最接近 目标 何确定      更新时间:2023-09-26

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

JavaScript 公式是什么来找到哪个数字,当乘以 0.75 时,最接近 2062 年?

PS:这里最接近的意思是等于(==)或非常接近,但低于目标数字,不是很接近但高于目标数字。

您正在寻找x * 0.75 = 2062 x。 所以解决应该x = 2062 / 0.75 x. 要确保该数字是最接近的整数小于或等于 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/