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

后端 未结 4 622
忘掉有多难
忘掉有多难 2021-01-30 00:40

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 va

相关标签:
4条回答
  • 2021-01-30 01:11

    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.

    0 讨论(0)
  • 2021-01-30 01:19

    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
    
    0 讨论(0)
  • 2021-01-30 01:33

    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;
    }
    
    0 讨论(0)
  • 2021-01-30 01:35

    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])
    
    0 讨论(0)
提交回复
热议问题