Home > database >  Calculate reputation percentage based on negative and positive
Calculate reputation percentage based on negative and positive

Time:01-06

I have users which can receive up-votes on their posts. Example: 5 users upvote 2 users downvote. I need to calculate the percentage of positive feedback.

I try with this code:

$result=(($positive-$negative)/$negative);
$r = ($result*100);
$result = $r / 100;

The code above does not work because if i have 50 upvotes and 1 downvote, The percentage results in 49%.

CodePudding user response:

If 50 votes is 100%, you need to calculate percentage for 49 votes. Here is the math:

/* 50(votes) / 100% = 49(votes) / $x% */
$x = 100 x 49 / 50;

Which is 98%

For 5 upvotes and 2 down votes you can use the same logic:

$x = 100 x 3 / 5;

Which is 60%

  •  Tags:  
  • Related