Operation in PHP. Multiplication using percentage

Asked

Viewed 982 times

5

<?php echo $i['sellingStatus'][0]['currentPrice'][0]['__value__']; ?>

How can I multipuple '__value__' by 2 and then add 60% to the total amount?

Example:

Valor = 10
Multiplica por 2 = 20
Acrescenta 60% = 20 * 60% = 12
Total = 20 + 12 = 32.

3 answers

8


Version "short":

<?php echo $i['sellingStatus'][0]['currentPrice'][0]['__value__'] * 3.2 ;>

Explanation:

Calculating 60% increment is the same thing as multiplying by 1.6.
After all 100% = 1, so 60% = 0.6.
Since we want increment, we want 100% + 60%, that is, multiplying by (1 + 0.6) => (1.6)

If you want to multiply by 2 and then add the 60%, it would be equivalent to

valor * 2 * 1.6

Simplifying

valor * 3.2

Version "long":

If you want to use the formula in several items with different percentages, then things change. You could use a function for that:

$indice = 2; // Acrescentado com base no comment, que vai ser o índice do dólar.
echo acrescimo( $i['sellingStatus'][0]['currentPrice'][0]['__value__'] * $indice, 60 );

function acrescimo( $valor, $porcentagem) {
   return $valor * ( 1 + $porcentagem/100 );
}
  • The short version I think worked better, because I’m still a little layman in PHP. The challenge now is: the value quoted as '2' will actually be how much the real is worth every 1 dollar.

  • @Gustavocave edited the answer to add the $indice.

  • I made a script to determine the value that will be multiplied by 60% (or 1.6). This value comes through the $value variable. In the <?php echo $i['sellingStatus'][0]['currentPrice'][0]['value']; ?> how should I do 'value' multiply by the value determined by the variable $value and then add 60% ?

  • @Gustavocave is what’s in the long version, only I called it $Indice. in the short version would be ...'value'] * $value * 1.6 (i.e., first multiplies by its $value, then puts 60% on top of the result)

  • Great explanation +1.

2

Try it like this, following the formula to figure out the percentage is: (numero1/100) * a porcentagem que você quer descobrir

    valor = 10;
    multiplicado = 10*2;
    porcentagem = (multiplicado/100)*60;
    total = multiplicado+porcentagem;

1

I will only supplement the previous answers, because both are absolutely right:

<?php
function acrescenta($valor, $percentual){
    return($valor * ( 1 + ($percentual / 100)));
}
?>

You use so (assuming the original value is 50):

<?php
$total = $total * 2;
$total = acrescenta(50, 60);
?>

Browser other questions tagged

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