-1

i need to set an integer to start from 001 instead of only 1

int iNumber = 001;
NSLog(@"%d",iNumber); // this is logging 1 instead of 001

is there a possible way to make it 001 ?

[UPDATED]

i need this because i`m creating an NSMutableArray from NSUserDefaults, after that I'm sorting the array using NSSortDescriptor. the problem is because i can't set the int to start from 001 the array i sorted like this 0, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9.

More Code explained

NSString *name = @"Name";
NSUserDefaults *MainArray = [NSUserDefaults standardUserDefaults];
NSString *Temp = [NSString stringWithFormat:@"%@%d", name,MainNumber];
[MainArray setObject:@"test" forKey:Temp];
[MainArray synchronize];
NSLog(@"%@",Temp);
MainNumber++;

the above code will save it like this : Name1,Name2,Name3....

i need it to be Name001, Name002, Name003 .......

thank you in advance :)

aLFaRSi
  • 559
  • 2
  • 12
  • 29
  • Please expand on why you want this behavior, or what exactly you want. In integer terms `001 == 1`. – blahdiblah Jun 20 '12 at 02:57
  • 2
    You are confusing _value_ with _appearance_. An `int` has a _value_ that can be _displayed_ in different ways. Some of the many ways to display the value `1` are: "001", "0x1", "1", "10e0". `int i = 1;` sets the value of `i` to "001". – jscs Jun 20 '12 at 03:28
  • You say something about sorting an NSMutableArray but you don't show any code related to sorting or to a NSMutableArray. You should check if this related post does what you want : http://stackoverflow.com/questions/2846301/how-to-do-a-natural-sort-on-an-nsarray – Taum Jun 20 '12 at 03:29

3 Answers3

3

Use %03d format specifier, e.g.

NSString *theString = [NSString stringWithFormat: @"%@%03d",name,MainNumber];
NSLog(@"%03d", iNumber );// can also use in nsstring stringWithFormat

The Objective-C way,

 NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
 [numberFormatter setPaddingPosition:NSNumberFormatterPadBeforePrefix];
 [numberFormatter setPaddingCharacter:@"0"];
 [numberFormatter setMinimumIntegerDigits:10];

 NSNumber * number = [NSNumber numberWithInt:1];
 NSString * theString = [numberFormatter stringFromNumber:number];

 NSLog(@"%@", theString);

Now your requiremnet:

 NSString *name = @"Name";
 NSUserDefaults *MainArray = [NSUserDefaults standardUserDefaults];
 NSString *Temp = [NSString stringWithFormat:@"%@%03d", name,MainNumber];
 [MainArray setObject:@"test" forKey:Temp];
 [MainArray synchronize];
 NSLog(@"%@",Temp);
 MainNumber++;
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • what i really need is to set the integer to 001 not the logging results :) – aLFaRSi Jun 20 '12 at 03:05
  • All you accomplish is creating a string with the value "001". Instead of doing all that you did, you could simply do this: `NSString *theString = [NSString stringWithFormat: @"001"];`. No offense, but your code is very excessive and not what the OP wants. – pasawaya Jun 20 '12 at 03:07
  • NSString *theString = [NSString stringWithFormat: @"%03d",iNumber]; – Paresh Navadiya Jun 20 '12 at 03:09
  • @qegal the original answer is exactly what the op wanted, you create a string that will be nsloged as 001 - 002 etc etc. the op could just create this string and append it as a string directly to name the file. the updated answer is more efficient tho. – Pochi Jun 20 '12 at 03:17
0

You're attacking the problem from too low a level. To create a sort descriptor that will sort the string @"10" after the strings @"2", @"3", @"4", etc., tell it to compare strings using localizedStandardCompare:, which

should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate.

[NSSortDescriptor sortDescriptorWithKey:theKey
                              ascending:YES
                               selector:@selector(localizedStandardCompare:)];
jscs
  • 63,694
  • 13
  • 151
  • 195
-3

You could use a 'float'

float *float = 001;

This may need tweaking as I am on my iPad, so I don't have Xcode to verify...

Allison
  • 2,213
  • 4
  • 32
  • 56