2016-04-28 43 views
-2

工作在我把用於計算價格這個代碼中的函數getPrecio

對於產品和價格低於1(例如0.2或0.02),此功能不起作用

爲什麼

此其代碼:?

function getPrecio($p) 
{ 
    if($p > 0 && $p <= 1) return $p*1.99; 
} 

它是一個PHP代碼 當我說不行那就是它不會更正計算的價格。

嘗試這個,而不是計算

function getPrecio($p) 
{ 
    $floatPrice = floatval($p); 
    if($floatPrice > 0 && $floatPrice <= 1) return $floatPrice *1.99; 
    //if($p > 0 && $p <= 1) return $p*1.99; 
    elseif($p > 1 && $p <= 3) return $p*1.95; 

更新的代碼

function getPrecio($p) 
{ 
    $floatPrice = floatval($p); 
    $floatCalculatedResult = 0; 
    if (between($floatPrice, 0, 1)) { 
     $floatCalculatedResult = $floatPrice * 1.99; 
    } else if (between($floatPrice, 1, 3)) { 
     $floatCalculatedResult = $floatPrice * 1.95; 
    } else if (between($floatPrice, 3, 5)) { 
     $floatCalculatedResult = $floatPrice * 1.90; 
    } else if (between($floatPrice, 5, 10)) { 
     $floatCalculatedResult = $floatPrice * 1.70; 
    } else if (between($floatPrice, 10, 20)) { 
     $floatCalculatedResult = $floatPrice * 1.60; 
    } else if (between($floatPrice, 20, 30)) { 
     $floatCalculatedResult = $floatPrice * 1.56; 
    } else if (between($floatPrice, 30, 50)) { 
     $floatCalculatedResult = $floatPrice * 1.52; 
    } else if (between($floatPrice, 50, 80)) { 
     $floatCalculatedResult = $floatPrice * 1.49; 
    } else if (between($floatPrice, 80, 100)) { 
     $floatCalculatedResult = $floatPrice * 1.45; 
    } else if (between($floatPrice, 100, 130)) { 
     $floatCalculatedResult = $floatPrice * 1.40; 
    } else if (between($floatPrice, 130, 200)) { 
     $floatCalculatedResult = $floatPrice * 1.39; 
    } else if (between($floatPrice, 200, 250)) { 
     $floatCalculatedResult = $floatPrice * 1.35; 
    } else if (between($floatPrice,250, 500)) { 
     $floatCalculatedResult = $floatPrice * 1.32; 
    } else if (between($floatPrice, 500, 700)) { 
     $floatCalculatedResult = $floatPrice * 1.28; 
    } else if (between($floatPrice, 700, 900)) { 
     $floatCalculatedResult = $floatPrice * 1.26; 
    } else if (between($floatPrice, 900, 1000)) { 
     $floatCalculatedResult = $floatPrice * 1.25; 
    } else if (between($floatPrice, 1000, 10000)) { 
     $floatCalculatedResult = $floatPrice * 1.15; 
    } 
    return $floatCalculatedResult; 
} 

function between($var, $from, $to) 
{ 
    return ($var > $from && $var <= $to); 
} 

更新時,如果0,02改變字段CSV〜0.02代碼工作。但是,因工作需要與0,02

+1

這是什麼編程語言?你的意思是「不起作用」? –

+1

獨立於這是什麼語言,也許你遇到了運算符優先級問題。是否(($ p> 0)&&($ p <= 1))'修復了它? – twalberg

+0

雙(())不解決問題 – vandark

回答

1

也許$p正確的代碼將被轉換爲int型,所以0.2或0.02將使用floatval($p)

function getPrecio($p) 
{ 
    $floatPrice = floatval($p); 
    if($floatPrice > 0 && $floatPrice <= 1) return $floatPrice *1.99; 
} 

更新答案

只是0嘗試這應該按預期工作。

function getPrecio($p) 
{ 
    $floatPrice = floatval($p); 
    $floatCalculatedResult = 0; 
    if (between($floatPrice, 0, 1)) { 
     $floatCalculatedResult = $floatPrice * 1.99; 
    } else if (between($floatPrice, 1, 3)) { 
     $floatCalculatedResult = $floatPrice * 1.95; 
    } 
    return $floatCalculatedResult; 
} 

function between($var, $from, $to) 
{ 
    return ($var > $from && $var <= $to); 
} 

您可以繼續與between功能

+0

謝謝你測試你的代碼和結果它一樣:)請幫助 – vandark

+0

@vandark更新了我的答案,這已經產生了正確的結果。總是使用你輸入的價格的'floatval()',所以你可以保留小數點後的所有內容。 –

+0

另外,你需要一個'else {return $ floatPrice; }'最後,所以你也處理這個分支。 –

0

添加在上面介紹的範圍:
$p = floatval(str_replace(',', '.', $p));

這將改變0,020.02

+1

已解決,感謝所有人!!!!! – vandark

相關問題