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

How to get percentile in "reverse" in php

I am not sure if this is correct title. I have let’s say 3000 members, each one is ranked based on number of points. Max rank is 3000, min rank is 1. I need to check for each member on which top 10% is the member (top 10%, top 20%, …, top 90%) based on their rank.

There are some answers how to get percentile, but these functions only check if the number is within percentile when you provide all numbers and percentile number. I need in reverse order, for example I have max rank number, min rank number and the member rank. All rank numbers are sequential. Now I need to get within which percentile is this number. Or more precisely within which 10%. Any idea how to get this?

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 :

Do you really need the min rank since you have a ranking which should obviously go to 1.

You need to be able to calculate the user percentage and then round it to the nearest 10%.

It could look like this:

function calculatePercentage($rank, $total){
    return ($rank/$total)*100;
}
    
function roundToNearestTen($value){
    return ceil($value / 10) * 10;
}

function getRank($rank, $total){
    return roundToNearestTen(calculatePercentage($rank, $total));
}

$rank = 99;
$total = 1000;

echo getRank($rank, $total).'%';
//Output: 10%
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