How can I pad an integer on the left with zeros?

Viewed 21824

I have the following variable:

NSNumber *consumption = [dict objectForKey:@"con"];

Which returns 42. How can I pad this number to 10 digits on the left, leading with zeros. The output should look as,

0000000042

or if it were 420,

0000000420

7 Answers

with variable num_digits

NSString* format =[NSString stringWithFormat:@"%%0%zdzd", num_digits];
NSString *theString = [NSString stringWithFormat:format, 42];
Related