22
$\begingroup$

So pardon if this is a simple question...

I have a slider that returns a value in a given range, so:

min: 174 max: 424 slider current value: 230 

I want to treat my min value as 0% and my max value as 100%. What formula can I use to calculate the percentage for my slider in this given range?

3 Answers 3

34

$ \frac{{n - 174}}{{424 - 174}} \times 100. $ In your example, $n=230$.

28

The full range is from $174$ to $424$. The distance between these is $424-174$, which is $250$.

The current value of the slider is $230$.This is $230-174$, that is, $56$ larger than the minimum possible value.

We are interested in the ratio $56/250$. A calculator shows that $56/250=0.224$. We want to express $0.224$ as a percentage. To do that, we multiply $0.224$ by $100$. The result is $22.4$. So the required percentage setting for $230$ is $22.4\%$.

Since we will not always be dealing with the setting $230$, let's generalize a little. Suppose that the slider setting is $S$. (In your question, $S=230$.) Assume that $174 \le S \le 424$.

Then the distance from $S$ to the minimum $174$ is $S-174.$ The ratio of this distance to the full distance $474-174$ is $\frac{S-174}{424-174}.$

To turn this into a percentage, multiply by $100$. The answer is $\left(\frac{S-174}{424-174}\right)\times 100 \:\:\%.$

Reality check: It is quite easy to make mistakes, I have done it hundreds of times. So let's see what answer we get from the above formula when $S=174$. Substitute $174$ for $S$. We get $0\%$. Good. Let's see what answer we get if we put $S=424$. Substitute. We get $100\%$. In general, spot checks of this kind do not guarantee that a formula is correct, but they are good evidence that we have not made a horrible blunder. (In linear cases, such as this one, two spot checks in fact do guarantee correctness.)

Now let us adjust the formula so that we can deal with a different situation, where the minimum setting is $m$ and the maximum setting is $M$. Let $m\le S \le M$. Then the percentage that corresponds to the setting $S$ is given by $\left(\frac{S-m}{M-m}\right)\times 100 \:\:\%.$ The reasoning that gives this formula is exactly the same as the one we used for the concrete numbers you supplied.

Now that we have a formula that gives the percentage $P$ when we know the setting $S$, we can use a little algebra to get a formula that gives the setting $S$ if we are told the desired percentage $P$. If you have need of such a formula, and have difficulty deriving it, please leave a message.

  • 0
    An extreme edge case that may need to be considered: if your range is truly arbitrary, code using this formula will need to avoid a divide by zero error should the upper and lower bounds be the same.2017-05-21
0

Wow, thank you, I tried to work this out for quite a while.

$B_2=MAX$

$A_1=MIN$

$D_1=\dfrac{B_2-A_1}{A_2-A_1}~$ — This is the result of your formula.

E1 = IF (D1 < 50%, -(1-2*D1), ABS(1-2*D1))

Using this I can use the above in Excel to get the exact $50\%$ as $0,$ and anything lower or higher generates a positive or negative $\%$ leading to the min or max numbers.

Using your numbers in your example $\begin{align} 299 &= 0\%\\ 174 &= -100\%\\ 424 &= 100\% \end{align}$

Hope this helps someone else.

  • 0
    Why do you make the scale $-100\%$ to $100\%$? The OP wanted the result to be between $0\%$ and $100\%$.2015-08-24