Make a two-digit string from a single-digit integer

aneuryzm picture aneuryzm · Jun 6, 2011 · Viewed 45.2k times · Source

How can I have a two-digit integer in a a string, even if the integer is less than 10?

[NSString stringWithFormat:@"%d", 1] //should be @"01"

Answer

highlycaffeinated picture highlycaffeinated · Jun 6, 2011

I believe that the stringWithFormat specifiers are the standard IEEE printf specifiers. Have you tried

[NSString stringWithFormat:@"%02d", 1];