129

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 = ?

2
  • inputPercentage = (inputValue/(rangeMax-rangeMin))*100 <-- this isn't working Commented Sep 14, 2014 at 16:51
  • 1
    If the range from 46 to 195 is supposed to be 100%, then you have to subtract 46 from 195 to “normalize” that range; and then the same for the value 65 … and after that, it’s just “normal” percentage calculation. Commented Sep 14, 2014 at 16:53

4 Answers 4

329

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 
Sign up to request clarification or add additional context in comments.

5 Comments

I needed to find the value in a range given the percentage. Taking this formula, I reworked it to val = ((percent * (max - min) / 100) + min
I suspect the formula can work when either one or both the range numbers are negative.
How would this work if the range has a negative number in either max or min?
@Daisy, well let's test it. If you're going from -10 to 10, then range is 10 - (-10) which is 10 + 10 and 20. Looks correct. Then assume input is 8. correctedStartValue would be 8 - (-10) = 18. That first looks strange, but lets keep going. The result would be (18*100) / 20 = 90%. And 90% looks fine to me for value 8 between -10 and 10.
@Daisy You only need to make sure that when both values are below 0, then max is the value with the largest absolute value. For example -50 and -20, then max=-50 and min=-20. Range will be -30, but that is fine. The negative sign is needed, since correctedStartValue will be negative as well.
3

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; } 

Comments

1

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.

Comments

0

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]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.