[英]Calculating a percentage position of Z between X and Y
Okay so if i have 3 values: 好吧,如果我有3个值:
x = 0;
y = 100;
z = 50;
The position of z
between x
and y
is 50%
. z
在x
和y
之间的位置为50%
。
If z
is 25
, the difference is 25%
. 如果
z
为25
,则差为25%
。
How can I work this out with javascript? 如何使用javascript解决呢? I want my return value to be like
0.5
or 0.25
我希望我的返回值像
0.5
或0.25
Thanks in advance from a first time StackOverflow user! 预先感谢第一个StackOverflow用户!
It's a simple mathematical formula, just use the floating point calculation: 这是一个简单的数学公式,只需使用浮点计算即可:
position = (z - x) / (y - x)
That will get you the relative position (0 through 1) of z
within the range x..y
, assuming x <= z <= y
and x < y
. 假设
x <= z <= y
和x < y
,您将在x..y
范围内获得z
的相对位置(0到1)。 If you want a percentage, simply multiply by 100. 如果您想要一个百分比,只需乘以100。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.