Question

I have an int and I transform it to a NSString like this:

NSString *myStr = [NSString stringWithFormat:@"%d",myInt];

myInt is ranged from 0 to 99999. What I want is to get myStr as a 5-digit number.

for example : 00072 or 09856 or 00002

Thank you.

Was it helpful?

Solution

Use the %05d format specifier in order to pad with zeroes:

NSString *myStr = [NSString stringWithFormat:@"%05d", myInt];

Wikipedia's explanation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top