Question

Having a mental blank, I need to calculate the percentage of a current number or position, based on it's position between two other numbers, example.

var start = 1;
var end = 20;
var currentPos = 10;

How can I calculate the currentPos value, based as a percentage, obviously it would be 50% but I'm wondering how to calculate this with any variables, example:

var start = 11;
var end = 20;
var currentPos = 12;

The end and start could potentially be the same value too

Was it helpful?

Solution

Assuming you won't have an end and start being the same and start<=currentPos<=end

var currentPercentile = (currentPos-start)/(end-start);

Update

if your end and start can be the same value then it can be 100, 0, or unknown since it'd be outside the range, your choice.

var currentPercentile = 100;  

if(end-start >0){
  currentPercentile = (currentPos-start)/(end-start);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top