Stephan König
Stephan König

Reputation: 143

Decimals in time conversion Objective-C

Here is my code:

//Umwandlung der Sekunden in Stunden / Minuten / Sekunden

NSUInteger sekunden = [[[self.parser.videosArray objectAtIndex:indexPath.row] videoLength] integerValue];
unsigned short int stunden = sekunden/3600;
sekunden -= stunden * 3600;
unsigned short int minuten = sekunden / 60;
sekunden -= minuten * 60;

cell.detailTextLabel.text = [NSString stringWithFormat:@"Dauer: %i:%i:%lu", stunden, minuten, (unsigned long)sekunden];

return cell;

}

It works fine so far without warning or errors. The only problem is, in the applications it shows for example Dauer: 0:4:3 instead of 00:04:03.

Does anyone know how I can fix this?

Thank you very much... :)

Upvotes: 0

Views: 130

Answers (1)

cnc_general
cnc_general

Reputation: 36

Your format string can fix the number of digits you want the output to have:

cell.detailTextLabel.text = [NSString stringWithFormat:@"Dauer: %02i:%02i:%02lu", stunden, minuten, (unsigned long)sekunden];

"%02i" means minimum of 2 digits padded with '0' character

Upvotes: 2

Related Questions