Calculate percentage php [duplicate]

1

I have two numbers, one of them is fixed ( 1700 ) and one will always be switching between 0 and 1700 , I want to calculate the percentage of that number that is alternating, for example, when the number is 1071 the percentage is equal to 63% , when it is 1700 it is equal to 100% !

$NumeroFixo = 1700
$Alternado = 1071

$Porcentagem = 63%

How can I do this?

    
asked by anonymous 28.11.2017 / 20:03

1 answer

7

It's more of a math problem than PHP.

Just do a rule of three and represent it in the code.

The rule of three is simple, I believe you already know, but I will leave the logic as a demonstration

Se 1700 é 100%, quantos % é 1071?

1700 --- 100
1071 ---  x

Multiplica-se as diagonais e tem-se:

1700x = 1071*100; -> 
1700x = 107100; -> 
x = 107100 / 1700; -> 
x = 63

Now, just change these values by the variable names and represent the expression in code.

$Porcentagem = ($Alternado * 100) / $NumeroFixo;

See working at Repl.it

    
28.11.2017 / 20:11