How to calculate percentage between the range of two values a third value is
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
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;
}
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.