How to calculate the percentage of Likes?

Asked

Viewed 442 times

1

How can I find a way to calculate two values to arrive at a percentage, for example: I have 100 Likes in a variable and 10 dislikes in another. In this example,(100 - 10 = 90) 90% of the people who read it liked it... This is a very simple example, but what if I had 921320201 Likes and 201312 dislikes, how I would perform this equation?

  • 3

    I must disagree with 100 Likes and 10 dislikes result in 90% acceptance. I would say it has 100/(100+10) = 90.909...% acceptance. This affects enough when the numbers are closer

2 answers

3


I applied the formula (likesPositives/totalLikes) * 100 = percentage

<?php 
$likesPositivos = 5000;
$dislikes = 2500; 
$totalLikes = $likesPositivos + $dislikes;

$porcentagem = ($likesPositivos/$totalLikes) * 100;

echo $porcentagem;
  • This example is wrong. Notice: Undefined variable: Likes in C: wamp64 www test.php on line 6

  • 1

    I edited the answer. Now it won’t trigger the error.

1

You want to know what the occurrence of a certain feature in a set. If every element has equal weight, the formula is:

Contagem de indivíduos com a característica / total de indivíduos 

In case, you have two disjoint types of individuals:

  • like
  • dislike

So we can turn the formula into:

likes / (likes + dislikes)

In this case, we will get a number between [0,1]. This is the ratio in absolute terms. To take in percentage terms, just multiply the result by 100.

Don’t forget to do the operation with real numbers instead of the entire division.

According to that post, PHP already works with real division by default. Second that post, Javascript works like this too.

Browser other questions tagged

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