# Calculate percentage given value, minim and maximum

It's my first time on Math.stack; be gentle.

I have slider with a range between -1 and 1. If my slider is at 0 I'd expect it to be at 0% If it were at either -1 or 1 I'd expect it to be 100% However it must take into account those won't always be the max & min

When I've got a minimum value of -0.1896362 and maximum value of 0.1383057 I get a bit confused

This is what I've got so far (This is wrong):

percentage = ((slider-minimum)/(maximum-minimum)) *100

I've read this post which is similar to my problem, but the negative numbers are messing things up.

• What is this supposed to be a percentage of? I'm so confused. And gentle. – Shahar Mar 20 '14 at 14:40
• If you must know, it's for converting PCM audio data (which ranges between -1 and 1) to a percentile value. – Mr Mystery Guest Mar 20 '14 at 14:45
• So you want to know the percent covered from 0 on the bar (i.e. - at x=-0.5, percent = 50%)? – Shahar Mar 20 '14 at 14:47
• Shouldn't it go from $0\%$ at the minimum $(-1)$ to $100\%$ at the maximum $(1)$? If so, your equation is fine and you are probably forgetting that minus a negative is plus. If I'm wrong, if the min is not the negative of the max do you truly want $0$ to be $0\%$? $0$ is no longer the center. – Ross Millikan Mar 20 '14 at 15:06

Alright, so let's take $u$ to be the upper bound. Lets make $l$ the lower bound. When you go to the right, the percentage of the area swept from x=0 to some $x$ the right is:

$$\frac{x}{u}\times 100\%$$

Similarly, on the left you'll just use your lower bound. You don't even need absolute value because the negatives will cancel:

$$\frac{x}{l}\times 100\%$$

Let me know if that's what you meant.

• So treat upper and lower bound as separate case? That looks good to me. Thank you. – Mr Mystery Guest Mar 20 '14 at 15:17

So the slider (call it $x$) goes from 100% at $x=-1$ to 0% at $x=0$ and 100% at $x=1$.

Then use the absolute value function to get these values:

$$\text{percentage} = |x| \cdot 100$$