views:

53

answers:

1

I am using objective c, and I got some variables like this:

1100
920
845
1439

But I want to change it to :

11:00 
09:20
08:45
14:39

The problem is how can I fill the zero when I got three number only? I know the logic can be simple to do it I detect the string length, but is there any way to do it more effectually? Thank you.

+5  A: 
unsigned int time = 1100;

NSString *strTime = [NSString stringWithFormat:@"%02u:%02u", time / 100, time % 100];
dreamlax
To get leading zeroes, both of the `%d` s will need to be changed to `%02d`.
David
@David: Yeah I realised that the moment I answered.
dreamlax
what is the different between 02u and 02d?
Ted Wong
@Ted Wong: `%u` is for *unsigned* integer decimal representation, whereas `%d` is for signed integer decimal representation.
dreamlax
time should be `unsigned int`. NSUInteger is not guaranteed to be the right length for %u.
JeremyP
@JeremyP: You're right, I thought I saw an `iphone` tag in the question.
dreamlax