简体   繁体   English

计算Z在X和Y之间的百分比位置

[英]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% . zxy之间的位置为50%

If z is 25 , the difference is 25% . 如果z25 ,则差为25%

How can I work this out with javascript? 如何使用javascript解决呢? I want my return value to be like 0.5 or 0.25 我希望我的返回值像0.50.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 <= yx < y ,您将在x..y范围内获得z的相对位置(0到1)。 If you want a percentage, simply multiply by 100. 如果您想要一个百分比,只需乘以100。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有较大坐标x / y / z的位置 - Position with large values of coordinates x / y / z 如何从Phonegap中的加速度计xyz值计算位置 - How to calculate position from accelerometer x y z values in phonegap 在JS中Lat Lat到XYZ位置..不工作 - Lat Long to X Y Z position in JS .. not working / x | y | z /的含义 - Meaning of /x|y|z/ 计算原点与 x,y,z 坐标之间的 θ 和 ø 角度 - Calculate θ and ø angles between origin and x,y,z coordinate 如何在 Shopify 上使用 JavaScript 或 jQuery 自动计算折扣金额 (X - Y) 并显示折扣百分比 [暂停] - How to automate calculating discount amount (X - Y) and show the percentage discount using JavaScript or jQuery on Shopify [on hold] [] .concat([“x”,“y”],“z”) - &gt;如何获得[[“x”,“y”],“z”]而不是[“x”,“y”, “Z”]? - [].concat([“x”,“y”],“z”) -> How to get [[“x”,“y”],“z”] instead of [“x”,“y”,“z”]? 时间动态时,相同的位置对于相同的x,y,z或lat,long,height显示不同 - same position shows differently for same x,y,z or lat,long,height when time-dynamic 当位置(x,y或z)太大时,mesh在three.js中摇动 - Mesh shakes in three.js when position (x, y or z) is too large number 有没有办法从 three.js 中的 function 参数中获取 position (xyz)? - Is there any way to get position (x y z) from a function parameter in three.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM