Baub
Baub

Reputation: 5044

Calculate Pace To String

I am trying to calculate pace (min/mi) and format it as mmmm:ss.

So far I calculate my pace into a float by taking 60 and dividing it by my average speed. At an average speed of 76mph, my average pace is displayed as 0.79. I want to format it so that it converts my 0.79 minutes to mmmm:ss (thus showing my average pace as 0000:47). How can I do this?

Upvotes: 0

Views: 313

Answers (2)

rob mayoff
rob mayoff

Reputation: 385870

double milesPerHour = 76.0;
int secondsPerMile = (int)round(3600.0 / milesPerHour);
NSString *paceString = [NSString stringWithFormat:@"%04d:%02d", secondsPerMile / 60, secondsPerMile % 60];

Upvotes: 5

Sascha
Sascha

Reputation: 5973

Not sure if I get your question right, but this is pretty much just math. You can get the minutes by rounding your value (0.79 in this case) down and you can get seconds by taking your value, subtracting the minutes from it and multiplying that by 60.

So if you'd need 2.35 minutes for a mile, you'd have 2 minutes and 0.35*60 = 21 seconds.

Upvotes: 2

Related Questions