0

How to add zeros in front of a string in Objective-C so that the length of the new string will be equal to a specific value? For example, if the specific length is 6,

  1 --> 000001
 12 --> 000012
123 --> 000123
...

Besides, I don't want to use loops. More elegant, more pleasure.

1 Answers1

0

I can't actually think an easier solution than this:

NSInteger yourNumber = 12; // or your random number...
NSLog(@"%@", [NSString stringWithFormat:@"%06ld", yourNumber]);

That presents the output on the console:

000012
halfer
  • 19,824
  • 17
  • 99
  • 186
holex
  • 23,961
  • 7
  • 62
  • 76