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
Asked
Active
Viewed 461 times
-2
-
4How exactly can a number be 0100? – Lily Ballard Oct 25 '11 at 03:40
-
Use an NSNumberFormatter as described in this: http://stackoverflow.com/questions/1448804/how-to-convert-an-nsstring-into-an-nsnumber – Alan Moore Oct 25 '11 at 03:43
-
1As an integral number "0100" = 100. I don't get it – bryanmac Oct 25 '11 at 03:48
-
@Alan Thanks, but I have tried this but if we pass "042", then the result would be 42 instead of 042. This think, I have to use the NSString for storing the values, instead of NSNumber as it denotes 042 as 42. For achieving my requirements, I need 042 as result, so i think only NSString is the solution – iHS Oct 25 '11 at 03:49
-
Are you maybe trying to store .0100? – Kenny Winker Oct 25 '11 at 03:50
-
Ah, I see. I believe you are right in your conclusion. – Alan Moore Oct 25 '11 at 03:51
2 Answers
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
-
-
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