Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate reputation percentage based on negative and positive

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%.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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%

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading