-2

How can I convert NSString into NSNumber. For ex. I have @"0100", I want to convert it into NSNumber and as per my requirements, the result should be 0100 not 100. Is there any way to achieve this or I have to use NSString instead on NSNumber

iHS
  • 5,372
  • 4
  • 31
  • 49

2 Answers2

2

What do you mean by you need a NSNumber with value 0100, instead of 100? Does it make a difference that doesn't agree with integer arithmetic? If so, then how, and if not, then do you mean that you have to output the number at some later time with the leading 0?

If so, then use the correct formatter while outputting the number, and don't worry about it before.

If you need to store numeric characters exactly then use a NSString. Validate it before accepting or using it. Turning it into a number is not what you want to or mean to do.

  • I want user to enter numeric only (passcode), and I want to compare it with already stored value. The already soted value can be either NSString or NSSNumber (decided at runtime), so I want the exact output. I think, I have to go for NSString only – iHS Oct 25 '11 at 03:46
  • @iHS you don't need to convert it to number for that. – zakishaheen Oct 25 '11 at 03:53
  • Thanks alot. Looks like, NSString is only the correct option in this scenario. Thanks – iHS Oct 25 '11 at 03:56
0

Please find below the following which will solve your problem. You can easily convert your string with int & display as you want.

NSString *str=@"0100";
    int myInt = [str intValue];
    NSLog(@"Int:%i",myInt);
Result  Int:100

I hope, this will solve you problem. Thanks.

Amit Singh
  • 2,644
  • 21
  • 20