How to calculate the percentage value between two points

This is probably a very stupid question, but ..

I have 3 values, for example, you can call them minX, currentX and maxX. im looking for a way to calculate the percentage maxX that currentX represents. for example, if maxX is 50, and minX is 40, and currentX is 45, then I want to get 50%, it's all pretty simple, but im problem is that one or more of my variables is a negative number.

Any help would be appreciated let me know if I don't explain myself well enough

+4
source share
3 answers
(currentX - minX) / (maxX - minX) 

Gives you a percentage, even if you use negative numbers (as long as maxX > currentX > minX ). Also make sure you are dealing with double / floats, not ints. otherwise you will need to make a throw.

+16
source

You will need some error checking to make sure this is a positive number.

if (X> = 0)

.. you are something

else

The value entered was less than 0

0
source

Unity has already made this function for you. Multiply by 100.

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.InverseLerp.html

0
source

Source: https://habr.com/ru/post/1493468/


All Articles