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

后端 未结 4 692
忘掉有多难
忘掉有多难 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: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;
    }
    

提交回复
热议问题