Reputation: 2733
I'm a little stumped on this one so appreciate the assistance of the experts on this forum, please.
My app has a text field where the user enters a 10 digit number, that number can start with a 0, e.g. 0727158880
I need to convert that number to an int so I can increment it by 1 each time my app goes through a loop of doing some stuff. But when I convert the string to an int it drops the leading 0 and returns just: 727158880
I "think" this is because it sees the leading 0 and thinks it is an octal number, but I don't want it to do that. Any ideas how I can keep the number as entered by the user and then incremement it?
Thanks!
Upvotes: 0
Views: 166
Reputation: 52622
This has nothing to do with octal numbers at all. You are converting a string to an integer. Integers are just numbers, they have no digits, they have no leading zero digits.
If you enter "man printf" into terminal it will tell you all the formats for printing numbers, and you can tell it to print an integer in 10 digits with leading zeroes.
Upvotes: 1
Reputation: 798
A number cannot have leading 0. Hence you know there is 10 digits number. A solution is
NSInteger number = [str integerValue];
number++;
NSString *newStr = [NSString stringWithFormat:@"%010d", number];
Upvotes: 0