Postage slab and exceptional

Asked

Viewed 29 times

-1

Good afternoon,

I’m doing a function that gives me back the amount of the postage to pay, where this is provided through intervals, ie:

Weight <= 1 Kg returns 3,00
Weight <= 2 Kg returns 4,00
Weight <= 3 Kg returns 6,00 ...
Up to Weight <= 30 Kg returns 20,00 ...

Only my problem is if the product weighs for example 31 kg this should take the size of 30 kg + size of 1 kg and add the two and so on.

Another example the product weighs 68.5, that is, it should be 30 kg +30 kg+1 kg and add up all values.

But I don’t know how to do this algorithm, can you help me? Thank you

  • 1 will be 3.00, 2 will be 4.00, 3 will be 6.00. From 4 to 19 will be what?

  • I didn’t understand why the -1, since the weight between 4 and 30 can be any value. But I was able to do the algorithm as intended.

  • it wasn’t me who gave the -1, I just asked the question above! Abs;

2 answers

0

Makes a while reducing weight:

$peso = 31;
$valor = 0;
while($peso >= 0){   
  if( $peso - 30 >= 0 ){
    $peso -= 30;
    $valor += 20;
  }
  if( $peso - 3 >= 0 ){
    $peso -= 3;
    $valor += 6;
  }
  if( $peso - 2 >= 0 ){
    $peso -= 2;
    $valor += 4;
  }
  if( $peso - 1 >= 0 ){
    $peso -= 1; 
    $valor += 3;
  }
}
  • this code is not working, but thank you for trying

  • Okay. If I have another idea, put it here :).

0

Make sure you do what you need.

$peso = 61;
$valor = 0;

while($peso != 0)
{  
 if( $peso >= 30)
{
    $peso -= 30;
    $valor += 20;
}
else if( $peso >= 3){
    $peso -= 1; 
    $valor += 6;
}
else if( $peso >= 2){
    $peso -= 1; 
    $valor += 4;
}
else if( $peso >= 1){
    $peso -= 1; 
    $valor += 3;
}

}

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.