29

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?

Srivatsan
  • 26,311
neezer
  • 535

3 Answers3

43

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

Shai Covo
  • 24,077
30

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.

André Nicolas
  • 507,029
  • 1
    +1 Wow, very detailed answer. Thanks, I really understand it now! – neezer Jul 15 '11 at 22:41
  • Could you maybe also give me the formula for calculating S when I already have P? I tried it myself, but algebra is really not one of my best talents ;) – Wim Haanstra Mar 04 '14 at 21:35
  • 2
    Write $P$ as a percent, like $55$. We have in general $P=100\frac{S-m}{M-m}$. Multiply through by $M-m$ and solve for $S$. We get $S=\frac{1}{100}\left(PM+100m-Pm\right)$. – André Nicolas Mar 04 '14 at 21:44
  • 2
    Thanks so much, this worked. For any developers out here, do not forget to make the (1/100) in to (1.0f/100.0f) otherwise the answer will always be 0 (or somehow: -0). – Wim Haanstra Mar 05 '14 at 06:39
  • What's the name of the formula and any reference link? – andyPaul Feb 25 '17 at 23:41
  • 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. – gth May 21 '17 at 06:13
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.

Lucian
  • 48,334
  • 2
  • 83
  • 154
Peter
  • 1