Reputation: 1187
I'm working in JavaScript. I have a graphic done and it is somewhat like this.
100 [=======||=======] 200
So basically this is a kind of mathematical problem. On the left and right side are two numbers, as a range. The 3rd number, in between this range will be defined and the || mark is moved according to this 3rd number.
So in this example the 3rd number is 150, which makes the || divider in the exact middle.
What I want is a way to calculate a percentage value from these 3 given numbers to align the || divider through a css left: xx%
property. How might I do this?
EDIT: I'm adding the fiddle as told by the users. http://jsfiddle.net/e2dLv/1/
any suggestions/critics about the code are welcome.
It is solved now as answered by Zeta. It may help someone.
Upvotes: 2
Views: 113
Reputation: 105995
This is basically linear scaling with an initial offset:
p = 100 * (value - min) / (max - min)
Note that this assumes that value
is in the interval [min, max]
and max != min
. This gives you the percentage as a number between 0
and 100
.
Upvotes: 4