Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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

share|improve this answer

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.

share|improve this answer
    
+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 4 at 21:35
    
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 4 at 21:44
    
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 5 at 6:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.