How to calculate percentage between the range of two values a third value is

Algorithm

Algorithm Problem Overview


Example:

I'm trying to figure out the calculation for finding the percentage between two values that a third value is.

Example: The range is 46 to 195. The value 46 would 0%, and the value 195 would be 100% of the range. What percentage of this range is the value 65?

rangeMin=46

rangeMax=195

inputValue=65

inputPercentage = ?

Algorithm Solutions


Solution 1 - Algorithm

Well, I would use the formula

((input - min) * 100) / (max - min)

For your example it would be

((65 - 46) * 100) / (195 - 46) = 12.75

Or a little bit longer

range = max - min
correctedStartValue = input - min
percentage = (correctedStartValue * 100) / range 

If you already have the percentage and you're looking for the "input value" in a given range, then you can use the adjusted formula provided by Dustin in the comments:

value = (percentage * (max - min) / 100) + min

Solution 2 - Algorithm

I put together this function to calculate it. It also gives the ability to set a mid way 100% point that then goes back down.

Usage

//[] = optional
rangePercentage(input, minimum_range, maximum_normal_range, [maximum_upper_range]);

rangePercentage(250, 0, 500); //returns 50 (as in 50%)

rangePercentage(100, 0, 200, 400); //returns 50
rangePercentage(200, 0, 200, 400); //returns 100 
rangePercentage(300, 0, 200, 400); //returns 50 

The function

function rangePercentage (input, range_min, range_max, range_2ndMax){

	var percentage = ((input - range_min) * 100) / (range_max - range_min);

	if (percentage > 100) {

		if (typeof range_2ndMax !== 'undefined'){
			percentage = ((range_2ndMax - input) * 100) / (range_2ndMax - range_max);
			if (percentage < 0) {
				percentage = 0;
			}
		} else {
			percentage = 100;
		}

	} else if (percentage < 0){
		percentage = 0;
	}

	return percentage;
}

Solution 3 - Algorithm

If you want to calculate the percentages of a list of values and truncate the values between a max and min you can do something like this:

private getPercentages(arr:number[], min:number=0, max:number=100): number[] {
  let maxValue = Math.max( ...arr );

  return arr.map((el)=>{
    let percent = el * 100 / maxValue;
    return percent * ((max - min) / 100) + min;
  });
};

Here the function call:

this.getPercentages([20,30,80,200],20,50);

would return

[23, 24.5, 32, 50]

where the percentages are relative and placed between the min and max value.

Solution 4 - Algorithm

Can be used for scaling any number of variables

Python Implementation:

# List1 will contain all the variables
list1 = []
# append all the variables in list1
list1.append(var1)
list1.append(var2)
list1.append(var3)
list1.append(var4)


# Sorting the list in ascending order
list1.sort(key = None, reverse = False)

# Normalizing each variable using ( X_Normalized = (X - X_minimum) / (X_Maximum - X_minimum)  )
normalized_var1 = (var1 - list1[0]) / (list1[-1] - list1[0])
normalized_var2 = (var2 - list1[0]) / (list1[-1] - list1[0])
normalized_var3 = (var3 - list1[0]) / (list1[-1] - list1[0])
normalized_var4 = (var4 - list1[0]) / (list1[-1] - list1[0])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser45675View Question on Stackoverflow
Solution 1 - AlgorithmTomView Answer on Stackoverflow
Solution 2 - AlgorithmDaniel TononView Answer on Stackoverflow
Solution 3 - AlgorithmRon JonkView Answer on Stackoverflow
Solution 4 - AlgorithmAmil SharmaView Answer on Stackoverflow