Zaki
Zaki

Reputation: 129

Displaying an integer from a string using the minute , second and isecond time format

Please can anyone help me out here, I wish to display the following:

   for (int i = 0; i < [highScores count]; i++)
    {
        [scoresString appendFormat:@"%i. %i\n", i + 1, [[highScores   objectAtIndex:i] intValue]];
    }

As a time value in this format 00:00:00 (in minutes seconds and hundredths of a second). At the moment I am getting values such as 7008(note that i have my maximum time in seconds is defined as *# define MAX_TIME 7200*).

Please how do about doing this conversion.

Upvotes: 0

Views: 545

Answers (1)

Nick Lockwood
Nick Lockwood

Reputation: 40995

As X Slash states, if your integer is in seconds then you can't get hundredths of a second, but to convert it to minutes and seconds, do this:

int total = [[highScores   objectAtIndex:i] intValue];
int minutes = total / 60;
int seconds = total % 60;
[scoresString appendFormat:@"%i. %02i:%02i\n", i + 1, minutes, seconds];

Note the 02 between the % and i - that means space out the number to two characters and insert leading zeros.

Incidentally, if your seconds are coming out as 7008 then with the logic above you'll get a time of 116:48 - i.e. 116 minutes, 48 seconds. Are you sure your time is in seconds? If it is you may wish to add an hours column.

Upvotes: 2

Related Questions