Split double-digit NSInteger into Two NSIntegers

239 views Asked by At

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.

4

There are 4 answers

0
Paulw11 On BEST ANSWER

You can simply use integer/modulo arithmetic

int tensDigit=originalNumber / 10;
int onesDigit=originalNumber % 10;
1
Rizwan Shaikh On

use this it work (In Swift).

var number  = 36

var tenPlace = number / 10

var UnitPlace = number % 10

see the Screen shot.

enter image description here

0
Peyman 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)

0
Akshay Karanth On
NSInteger a=11;
NSInteger b=a%10;
NSInteger c=(a-b)/10;
NSLog(@"%@ %@ %@",a,b,c);