2011-08-12 667 views
13

我正在为一家面料商店构建一个相当耗费计算时间的购物车,并且发现自己需要对用户输入的长度*每米的基本价格进行计算,但随后检查结果以查看它是否是模式长度的倍数。如果它不是一个倍数,我需要找到模式长度的最接近的倍数并将结果更改为该倍数。javascript如何判断一个数字是否是另一个数字的倍数

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

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

任何帮助,不胜感激

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

+3

不要使用浮点运算的钱。将价格转换为美分。 –

+0

我会的,一切都是浮点atm,因为这就是它是如何出来的分贝:( – jammypeach

回答

24

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

Ex。

//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

facepalm :)我虽然模数,但没有想通过我可以做的余下。这对我很有用,非常感谢。 – jammypeach

+0

嘿,我不想写一个重复的,你的意思是如果'b'是'a'的乘数,你会为'if(a%b){}'写一个快速'bool'来返回true。编辑:下面的答案更好地解释我的情况。 – thednp

16

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

//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乘以首既然后再进行上述检查。

+5

只记得四舍五入的数字,所以你不会得到像http://stackoverflow.com/questions/3966484任何浮点问题/ floating-point-numbers-and-javascript-modulus-operator – Teodor

3

在JavaScript中有剩余的运算符(类似于大多数C语言的语言)。

令x =长度和y =价格和X * Y的Z =产品

var remainder = (z % x)/100; 

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

要获得最接近X多到你的结果±您可以使用小区四舍五入的结果向上(或向下)或数学库中的底层函数。

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

然后圆到小数点后两位

Math.round(a/100) * 100; 
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; 
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); 
相关问题