2

How can I add a zero to the left of a 1 digit integer ? Is there any objective C function to perform this? I am needing this so I can have only one NSDateFormat @"ddMMyyyy" thanks

I want to add a zero to the left of an integer which is less than 10, I don't want to use an if statement. Is there any function or way to achieve this?

HoNgOuRu
  • 717
  • 4
  • 13
  • 26
  • 1
    Please be more clear. Do you want to prepend a '0' to the string representation of an integer? (e.g. (int) 1 => "01") ? – Coleman S May 30 '11 at 20:28
  • Similar to [Format string, integer with leading zeroes](http://stackoverflow.com/questions/2985170/format-string-integer-with-leading-zeros). – Jeremy W. Sherman May 30 '11 at 20:29
  • also thank u, my question was answered correctly. – HoNgOuRu May 30 '11 at 20:39
  • possible duplicate of [Padding string to left with objective c](http://stackoverflow.com/questions/964322/padding-string-to-left-with-objective-c) – nevan king May 30 '11 at 20:40

2 Answers2

29

Use the string format specifier %02d and any single digit integer will be padded with a zero in a string.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
1

You can use string formatters to add padding to your integer value as shown here: Help in padding numerical strings

Community
  • 1
  • 1
Jonah
  • 17,918
  • 1
  • 43
  • 70