0

I have a healthbar that i want to use a min/max and actual number.
6 is min
8 is max
the actual number will float between them with 7 as the beginning number.
tried ((max_n + min_n) / act_n)*100, but of course that doesn't work.
i think i was sick that day when they went over that in school (eyeroll)

Jeffiec_c
  • 55
  • 7

2 Answers2

4

If you have a floating point value that is (6, 8) and you want the distance between the value and minimum expressed as a different between the maximum -

var min = 6, 
    max = 8, 
    current = 7, 
    difference = max - min, 
    percent = (current - min) / difference;  // <-- is the value you're after
                                             // it is 50% between (6,8)
jdphenix
  • 15,022
  • 3
  • 41
  • 74
1

I'm not sure I understand what you're doing, but I believe you're calculating the average the wrong way. Shouldn't you have the actual value be divided by the total possible?

Currently, your logic looks like this: (8 + 6 (14) / Actual (7) )*100, which would be 14/7 = 2 * 100 = 200.

I think you need to reverse these two arguments, the min+max and the actual value.

If it were me, instead of doing it that way, I would actually subtract the minimum amount from both, and then divide it by the difference between the two. If the 6 and 8 are dynamic, this may look like:

( (act_n - min_n) / (max_n - min_n) )*100

Or, with values, that would be: ( (7.2 - 6) / (8 - 6)*100

(1.2 / 2)*100 = 60, so your percentage would be 60%

I don't use Javascript, so please ignore any spacing errors I made. I don't know how strict the language is.

schizoid04
  • 888
  • 1
  • 11
  • 27