javascript如何判断一个数字是否是另一个数字的倍数

时间:2011-08-12 09:08:16

标签: javascript jquery math

我正在为一家面料商店建造一个计算量很大的购物车,并且发现自己需要对用户输入的长度*每米的基本价格进行计算,然后检查结果以查看它是否为图案长度。如果它不是倍数,我需要找到最接近的模式长度的倍数,并将结果更改为。

我还需要能够在PHP中完成相同的计算,但如果有人可以帮我解决数学问题,我可以移植任何需要自己翻译的内容。

我正在使用jQuery 1.6.2并且已经完成了计算的第一部分,我只需要根据模式长度检查(米*价格)的结果。

任何帮助非常感谢

编辑:这些计算都涉及价格和模式长度的2位小数。用户输入的长度也可能包含小数。

6 个答案:

答案 0 :(得分:27)

在Javascript和PHP中使用%(模数)运算符,它在ab除以a % b时返回余数。当ab的倍数时,余数将为零。

实施例

//Javascript
var result = userLength * basePrice;     //Get result
if(result % patternLength){              //Check if there is a remainder
  var remainder = result % patternLength; //Get remainder
  if(remainder >= patternLength / 2)      //If the remainder is larger than half of patternLength, then go up to the next mulitple
    result += patternLength - remainder;
  else                                    //Else - subtract the remainder to go down
    result -= remainder;
}
result = Math.round(result * 100) / 100;  //Round to 2 decimal places

答案 1 :(得分:23)

您可以使用模数来找到除法后的余数,然后如果余数等于零,则它是一个倍数。

//x and y are both integers
var remainder = x % y;
if (remainder == 0){
//x is a multiple of y
} else {
//x is not a multiple of y
}

如果您使用的数字可能是2dp,那么模数应该仍然有效,如果不是,则先将它们乘以100然后再进行上述检查。

答案 2 :(得分:3)

在javascript中有余数运算符(类似于大多数具有类c语法的语言)。

设x =长度,y =价格,z = x * y的乘积

var remainder = (z % x) / 100;

if (remainder === 0) {
   // z is a multiple of x
}

要获得与结果z最接近的x倍数,您可以使用数学库中的ceil或floor函数向上(或向下)舍入结果。

if (r >= x / 2) {
    a = Math.ceil(z/x)*x;
}
else {
    a = Math.floor(z/x)*x;
}

然后舍入到小数点后两位

Math.round(a / 100) * 100;

答案 3 :(得分:1)

不确定我是否真的理解这项任务,因为对我来说这似乎很简单,但看看这个PHP代码:

// --- input ---
$pattern = 12.34;
$input = 24.68;
$precision = 2; // number of decimals

// --- calculation ---

// switch to "fixed point":
$base = pow(10, $precision);
$pattern = round($pattern * $base);
$input = round($input * $base);

if ($input % $pattern) {
  // not an exact multiple
  $input = ceil($input / $pattern) * $pattern;
} else {
  // it is an exact multiple
}

// back to normal precision:
$pattern /= $base;
$input /= $base;

这可以很容易地翻译成JavaScript。

$input将是该模式的下一个最接近的倍数。 如果你只是需要它而不需要知道它是否真的是一个倍数,你也可以简单地做这样的事情:

$input = ceil($input * 100 / $pattern) * $pattern / 100;

答案 4 :(得分:1)

这避免了JavaScript精度问题。

function isMultiple(x, y) {
  return Math.round(x / y) / (1 / y) === x;
}

console.log(isMultiple(2.03, .01))
console.log(isMultiple(2.034, .01))
console.log(isMultiple(.03, .01))
console.log(isMultiple(240, 20))
console.log(isMultiple(240, 21))
console.log(isMultiple(1, 1))

答案 5 :(得分:0)

//roundDown to 2 decimal places function
function r2(n) {
   return Math.floor(n*100)/100; 
}

neededLength = 4.55;
price = 4.63;
patternLength = 1.6;

// price despite the length of the pattern
priceSum = r2(neededLength * price);

// calculate how many pattern lengths there must be to fit into needed length
patternLengths = Math.floor((neededLength+patternLength)/patternLength);
// calculate price for pattern lengths
priceByPatternSum = r2((patternLengths * patternLength) * price );