Calculate php percentage

Asked

Viewed 1,708 times

1

I have two numbers, one of them is fixed (1700) and one will always be alternating 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 1700 is equal to 100%!

$NumeroFixo = 1700
$Alternado = 1071

$Porcentagem = 63%

How can I do that ?

  • Is confused: 1071 a porcentagem seja igual a 63%, quando for 1700 seja igual a 100% how so?

  • 1071 is 63% of 1700!

  • @Fox Is that example using similar numbers induces error.

  • this one already has an answer here https://answall.com/questions/199586/duvida-calculo-de/199616#199616

1 answer

7


It’s more of a math problem than PHP.

Just make a rule of three and act in the code.

The rule of three is simple, I believe you already know, but I’ll leave 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 swap these values by variable names and represent the expression in code.

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

See working on Repl.it

  • ah tendi, Thank you!

Browser other questions tagged

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