-2

I put this code for calculate price in a function getPrecio.

For products with prices below 1 (for example 0.2 or 0.02), this function does not work.

Why?

This its the code:

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

its a PHP Code. when I say that does not work it is that it does not correct the calculated price.

try this and not calculate

    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;

updated code

    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);
}

UPDATED, IF CHANGE THE FIELD CSV OF 0,02 TO 0.02 THE CODE WORKS. But need a correct code for work with 0,02

vandark
  • 1
  • 2

2 Answers2

1

Maybe $p gets converted to an int, so 0.2 or 0.02 will be just 0. Try using floatval($p)

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

Updated answer

This should work as intended.

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);
}

You can continue to introduce ranges with the between function

Alex Szabo
  • 3,274
  • 2
  • 18
  • 30
  • thanks y test your code and the results its the same :) Please help – vandark Apr 29 '16 at 07:32
  • @vandark updated my answer, this has produced me the correct results. Always use the `floatval()` of the price you input, so you can keep whatever's after the decimal point – Alex Szabo Apr 29 '16 at 07:48
  • Also, you would need to have an `else { return $floatPrice; }` at the end, so you handle that branch as well. – Alex Szabo Apr 29 '16 at 07:49
  • i updated the code, in prices upper 1 your code worsk but in prices 0.36 for example or 0.02 not. – vandark Apr 29 '16 at 08:33
  • I've tested it, and the function for 0.02 returns 0.0398, which is equivalent to 0.02 * 1.99, I don't see the error here. Can you please elaborate? – Alex Szabo Apr 29 '16 at 08:38
  • Or one thing you could try is this at the end of the `getPrecio()` function: `return floatval($floatCalculatedResult);` – Alex Szabo Apr 29 '16 at 08:39
  • I find the problem, in the csv the cost price its "0,02" if change manually to 0.02 your code works perfectly. – vandark Apr 29 '16 at 08:54
0

Add at the top:
$p = floatval(str_replace(',', '.', $p));

This will change 0,02 to 0.02.

Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57