PDA

View Full Version : Label always two digits




DIGITALiam
Jul 23, 2010, 03:42 PM
Hey I'm making a timer app that has both minutes and seconds. To do this I am using two different labels. How can I make sure it is always two digits (10:01 not 10:1).

Thanks in advance.



SilentPanda
Jul 23, 2010, 03:45 PM
If the number is less than 10 put a 0 in front of it. Just always convert the int to NSString and put it in the label that way.

DIGITALiam
Jul 23, 2010, 03:55 PM
If the number is less than 10 put a 0 in front of it. Just always convert the int to NSString and put it in the label that way.

I thought of that too but it seems like there would be a more official way of doing it.

SilentPanda
Jul 23, 2010, 03:57 PM
The only other reasonable way I can think of doing it would be to use a formatter. But admittedly I'm still working my way through the API so that might not be viable.

dejo
Jul 23, 2010, 05:10 PM
I thought of that too but it seems like there would be a more official way of doing it.
What have you tried so far?

PhoneyDeveloper
Jul 23, 2010, 05:25 PM
NSString* s = [NSString stringWithFormat:@"%02d", someInt];

man printf

http://www.opengroup.org/onlinepubs/009695399/functions/printf.html

DIGITALiam
Jul 23, 2010, 06:07 PM
NSString* s = [NSString stringWithFormat:@"%02d", someInt];

man printf

http://www.opengroup.org/onlinepubs/009695399/functions/printf.html

Thanks a lot that code works. I didn't get to reading through that page you linked to put the code did the trick. I'll use it even if I don't understand it.:rolleyes:

dejo
Jul 23, 2010, 06:38 PM
I'll use it even if I don't understand it.:rolleyes:
Bad idea. Copying-and-pasting is not programming.