John
John

Reputation: 10156

How to convert minutes to percentage?

Im trying to find out a percentage for a progress bar based on the max minutes given and how many minutes they have been waiting. For example, user says they will wait for 45 minutes, but it ended up being 2 hours. How can I convert that into a percentage?

In this case I thought this would work:

minutes waited / max minutes * 100

But in this case minutes waited is 120 divided by 45 then times hundred gives me 267, rounded it. Would I need an additional check to see if over 100 then just use 100% as the progress bar number?

Upvotes: 2

Views: 8379

Answers (4)

Lefteris E
Lefteris E

Reputation: 2814

newMaxMinutes = timeSoFar / percentageOfWorkSoFar
progress = percentageOfWorkDoneSoFar

Upvotes: 0

Richard Pianka
Richard Pianka

Reputation: 3366

120 is 267% of 45. Depending on the language, you could use a ternary operator:

double ratio = (waited / max) * 100.0;
double percent = ratio > 100.0 ? 100.0 : ratio;

I agree with Graham though, it should be recalculated on the fly.

Upvotes: 0

user28636
user28636

Reputation: 192

This will give you the percent capped at 100, without an additional check. Min(100, minutes waited / max minutes * 100)

Upvotes: 0

Graham Christensen
Graham Christensen

Reputation: 849

Technically it is more than 100% of the time, so you would need to implement a final check to see if it is greater than 100%, and if so change it back to 100%.

I would also recommend (if applicable) recomputing the time remaining on the fly, so they never see 100% when in reality they still have another 45 minutes left.

Upvotes: 1

Related Questions