Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate value percentage value between two points

Tags:

c#

math

unity3d

This is probably a really dumb question but..

I have 3 values for example lets call them minX, currentX and maxX. im looking for a way to calculate the percentage of maxX that currentX represents. e.g if maxX is 50 and minX is 40 and currentX is 45 then i want to get 50% this is all pretty basic but the problem im having is when one or more of my variables is a negative number.

Any help would be appreciated, also let me know if i didn't explain myself well enough

like image 712
kgpmurray Avatar asked Jul 25 '13 15:07

kgpmurray


1 Answers

(currentX - minX) / (maxX - minX)

Will give you the percentage, even if you're using negative numbers (as long as maxX > currentX > minX). Also make sure you're dealing with doubles/floats, not ints. otherwise you'll need to cast.

like image 144
Tim Avatar answered Oct 15 '22 08:10

Tim