2011-08-12 666 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); 
相關問題