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

一个人想着一个人 提交于 2021-02-05 12:39:15

问题


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


回答1:


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



回答2:


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



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/25835591/how-to-calculate-percentage-between-the-range-of-two-values-a-third-value-is

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!