I'm not sure if my question is well formulated or not (the title), but basically. I have a min and max value, min is 50 and max is 200.

To calculate the percentage 50 is of 200, I do:

$$\frac{50}{200} \cdot 100 = 25\%$$

Now I need that 25 percentage value to be a percentage of the number 16.

I came up with the following formula:

$$\frac{25}{100} \cdot 16$$ Is that correct?.. I think it is.


To clarify, I'm writing a game and my 'player' entity has a current health value and a maximum health value.

To draw a health bar I need the current health percentage to the max health.

But I can't set that bar as 100% width. the graphical bar is a range from 0 to 16. 16 being maximum health.

  • $\begingroup$ What do you mean by "percentage to absolute value within another range"? Where did $16$ come from? Maybe you ought to explain where did this problem come from. $\endgroup$ – Vedran Šego Sep 21 '13 at 1:23
  • $\begingroup$ What would be that percentage of $50$? What would be that percentage of $100$? What would be that percentage of $200$? $\endgroup$ – Vedran Šego Sep 21 '13 at 1:24
  • $\begingroup$ I have edited the question to clarify. $\endgroup$ – Petter Thowsen Sep 21 '13 at 1:26

O.K., if I got right from your explanation, you have three values:

  • $min$ - the position on the bar (or on the screen) when the health is zero,
  • $max$ - the position on the bar (or on the screen) when the health is $100\%$,
  • $p$ - current health percentage, between $0$ and $100$.

So, what you need is where to draw a line between $min$ and $max$ to represent $p$. The solution is

$$min + \frac{p}{100} \cdot (max - min).$$

In case you want $h$ between $0$ and $16$ instead of $p$, you just use

$$min + \frac{h}{16} \cdot (max - min).$$

I hope I understood you right.

| cite | improve this answer | |

In some programming languages or systems, this particular idea is given its own construct and terminology, just like multiplication or division, and it is often called a "ratio" or "reset", especially in the world of digital control systems (HVAC for example).

The ratio or reset function requires five data points: the min and max for both the incoming and outgoing ranges, and the current incoming value. Then there are certain options that can be applied, such as endpoint clamping vs. extending the range, and these change what happens outside the ranges you have defined.

The main function then looks like the following, where $i$ is the input value, $m,n$ are the input max and min, and $s,t$ are the output max and min:

$$r(i)=(i-n){s-t\over m-n}+t$$

To apply output min/max clamping, use:


Note that both functions will not return valid results if either $m=n$ or $s=t$.

In your scenario, you have $n=50,t=0$, so the formula for the position given the percentage is:

$$r(p)=p\cdot{200-50\over 100}+50$$

Alternatively, the formula for the percentage given the position would be:

$$r(q)=(q-50){100\over 200-50}$$

The health bar and the percentage are in direct proportion, so you can apply the following:

$$h=p\cdot \frac{16}{100}$$

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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