rangeToPercent() and percentToRange()

These are two very helpful JavaScript functions if you need to work with ranges and percents.


function rangeToPercent(number, min, max){
   return ((number - min) / (max - min));

This function returns percentage of a number in a given range. For example rangeToPercent(40,20,60) will return 0.5 (50%). rangeToPercent(34,0,100) will return 0.34 (34%)


function percentToRange(percent, min, max) {
   return((max - min) * percent + min);

This function returns number that corresponds to the percentage in a given range. For example percentToRange(0.34,0,100) will return 34.



Original functions are part of  MathUtils Class by Bruno Imbrizi written in ActionScript3.

Leave a Reply