I am trying to figure out how to take a double-digit NSInteger on iOS (like 11) and turn it into 2 separate NSIntegers, each looking like "1". It will always be just 2 digits in the NSInteger, no more, no less, and no decimals.
Split double-digit NSInteger into Two NSIntegers
239 views Asked by user717452 At
4
There are 4 answers
0
On
You can simply divide by 10 and find the reminder of division by 10.
NSNumber* number = [NSNumber numberWithInt:21];
NSNumber* partOne = [NSNumber numberWithInt:([number integerValue] / 10)];
NSNumber* partTwo = [NSNumber numberWithInt:([number integerValue] % 10)];
for 21, partOne will be 2 and partTwo will be 1.
In general, for any number n, ith digit is n % pow(10, i)
You can simply use integer/modulo arithmetic