0

I am trying to convert an int to a string in objective-C.

I read the other questions on SO about converting ints to strings, and I tried this method in my code:

-(void)setCounter:(int)count
{
    counterText.text = [NSString stringWithFormat:@"%d",count];
}

However, if I want to display a number like '01' the 0 is taken out of the conversion and only '1' is displayed. Is there a workaround?

snowflakekiller
  • 3,428
  • 6
  • 27
  • 45
  • possible duplicate of [Printing leading 0's in C?](http://stackoverflow.com/questions/153890/printing-leading-0s-in-c) – Caleb Dec 20 '11 at 04:59
  • Not 100%, since `stringWithFormat:` doesn't exist in C, and `sprintf` answer would produce a wrong type (although the format spec is the same). – Amadan Dec 20 '11 at 05:02

4 Answers4

10

There is no such number as 01. If you write

int count = 01;

it is compiled equivalently to

int count = 1;

In fact, be careful: 07 is equivalent to 7, but 011 is equivalent to 9!

What you can do is ask stringWithFormat: to give you the zero-padding:

[NSString stringWithFormat:@"%02d",count]

should give you "02" if count is 2. To deconstruct it:

% - interpolate the next value here
0 - pad it to the width by placing zeroes on the left side
2 - width is 2 characters
d - it will be an integer. Do it now.
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 3
    Lucky you didn't use the example: `int count = 08;` :-) – paxdiablo Dec 20 '11 at 04:52
  • I see... Do you know of a way to get 01 to display then, without using an int? What I am trying to do is create a flip card counter display, and I need a way to pass it numbers to display – snowflakekiller Dec 20 '11 at 04:53
  • @Karuna-bdc: Yes, it's called using strings. Although if its' a flip card counter, it sounds like it'll always want to display 2 digits, and Amadan's already shown you how to do that. – Lily Ballard Dec 20 '11 at 04:54
  • @paxdiablo: Haha... Yeah, I remembered to put it in! – Amadan Dec 20 '11 at 04:56
  • @Amadan, my lousy internet connection lopped off the second part of your answer, but I got it now. Works perfectly and thanks for the explanation – snowflakekiller Dec 20 '11 at 04:59
  • @Karuna-bdc: Probably not your fault. I'll often write a very short answer, then think "oh, I could add this", or "hmm, I wonder if it's understandable enough", and go back to add more details. – Amadan Dec 20 '11 at 05:05
  • Amadan, just one idea for improvement. I'm not entirely certain ObjC is the same as C printf, but the width specifier is the _minimum_ width: printf "%02d" on 12345 will still give you 12345. – paxdiablo Dec 20 '11 at 07:23
  • @paxdiablo: ObjC `[s]printf` is the same as the C `[s]printf`, but `sprintf` prints into a `char[]`, while presumably the OP's `counterText.text = ...` requires an ObjC `String` (which can be obtained by `[NSString stringWithCString:... encoding:NSUTF8StringEncoding]` or by `[NSString stringWithFormat:...]`. You are, of course, correct about overflows. – Amadan Dec 20 '11 at 07:31
1

If you want a different format to the one shown, use it:

counterText.text = [NSString stringWithFormat:@"%02d",count];

There are a huge range of possibilities with the format string.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

if any number start from 0 then Its a octal representation (0 - 7). you can add zero explictly using below line.

  counterText.text = [NSString stringWithFormat:@"0%d",count];
iOSPawan
  • 2,884
  • 2
  • 25
  • 50
0

Check out..

NSNumber +numberWithInt

and then: NSNumberFormatter -setMinimumIntegerDigits

and then get the string representation with: NSString -stringFromNumber

Stephen Blinkhorn
  • 637
  • 2
  • 7
  • 17