Break a number into equal parts

Asked

Viewed 86 times

4

I have the variable $peso ranging from 1000 to 50000 (grams)

There is a table of freight I have as

1000a2000 => 10,00  
2000a3000 => 20,00  

...

9000a10000 => 100,00  
kg_adicional => 2,25 

Have ifs up to 10000(grams), above that I should calculate the additional value, for example

$peso = 18000

I have to take the value of 9000a10000 (R$ 100,00) and add another 8 KG, but I don’t know how to break this variable and do the validation.

  • 1

    How is your table ? How do you pull this data ? Array ?

  • (KG - MAXIMUM VALUE) * 2,25.

  • What do you mean quebrar?

  • You speak of breaking "9000a10000" (for example), into two variables or an array with size 2?

  • 3

    if ($weight>10000) { $total = 100+ ($weight-10000)*2.25}. Your question is in the formula??

1 answer

1


If the quantities are proportional, it is not necessary to use ifs:

1000a2000 => 10,00  
2000a3000 => 20,00  
3000a4000 => 30,00  
4000a5000 => 40,00  
5000a6000 => 50,00  
6000a7000 => 60,00  
7000a8000 => 70,00  
8000a9000 => 80,00  
9000a10000 => 90,00 (aqui é 90 ou 100?)

In theory, the following formula already solves.

$peso_excedente = $peso - 10000;
$peso_nominal = $peso - $peso_excedente;
$total = (floor($peso_nominal / 1000) * 10) + (floor($peso_excedente / 1000) * 2.25);

Browser other questions tagged

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