# Calculate percentage of point in range that includes negative numbers

I'm writing a computer program with a user interface that contains sliders with differing ranges. The range can be only positive, e.g. [1, 2], or contain negative values, e.g. [-1,1].

So I need to be able to calculate the percentage of a number inside of a range, where the numbers can be negative or positive.

For example I can have a range [-127, 127], and if the value is 0, it would be 50%.

Another example using only positive numbers would be [0, 127], where 0 would be 0%, but 63.5 would be 50%.

I would also like to be be able to calculate a number on a range from a percentage, so I think this would be the inverse.

I've been able to write functions that work for example 1 or 2, but not both. Introducing the negative numbers seems to add a lot of complexity (at least or me!)

Many thanks.

You're looking for $\frac{\text{value}-\text{min}}{\text{max}-\text{min}}$

e.g. where is $15$ in $[-127,127]$? It's $\frac{15-(-127)}{127-(-127)}=\frac{15+127}{127+127}=0.559$ (or 55.9%) of the way between the min and max.

• Excellent formula, (excuse my mathematical ignorance) but how to get the way around, we have the percentage and we need the value ? Commented May 17, 2021 at 21:02
• Start with thr equation as I have it ... proportion = (value - min)/(max -min). Multiply both sides by the denominator (max - min) and then add min to both sides. Commented May 18, 2021 at 5:30
• Thanks again, I like your solution simple and straightforward ... here is what worked for me value = proportion * (max - min) + min ... you may edit your answer and add it, helping others like me ;p Commented May 18, 2021 at 9:29

A simple way is to offset the range so that the lower limit is $0$. If you have the range $[-12,27]$ you can just add $12$ to all the numbers and consider the range to be $[0,39]$. To get the percentage of $5$, you add $12$ to it and find the percentage to be $\frac {17}{39}\cdot 100\% \approx 43.6\%$ For the inverse, you apply the percentage and apply the offset, so if you are given $65\%$ on the range $[-12,27]$ you find $65\% \cdot 39 =25.35,$ then subtract $12$ to get $13.35$

Multiply the percentage by the total range so, in the first case, it is $127-(-127)$. Then add it to the lower bound of the range, so $-127$.

If you follow this process, it will work to find the number in any range. So that is, find the difference between the maximum and minimum values, then multiply this by the percentage, then add it to the minimum value.