C# – How to calculate value percentage value between two points

cmathunity3d

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

Best Answer

(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.