George Ortiz George Ortiz - 3 months ago 12
PHP Question

Map Range of Numbers to 0 through 100

I have a possibility of numbers between +/- 6 including 0 (ie. 6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6) that come out of a ranking algorithm I have in PHP.


Instead of returning +/- 6, I would like to return a number between 0 to 100. The correlation would be similar to:

100 = +6

..

75 = +3

..

50 = 0

..

25 = -3

..

0 = -6


Considering the output range of the ranking algorithm, how would I programmatically achieve this in PHP? I've considered the following but am unsure as to the best approach:

function score_alg($x) {
if ($x == '6')
return 100;
if ($x == '3')
return 75;
if ($x == '0')
return 50;
if ($x == '-3')
return 25;
if ($x == '-6')
return 0;
}

Answer

This would work:

function score_alg($x) {
  return round(($x+6)*(100/12));
}
Comments