Tyler McMaster
Tyler McMaster

Reputation: 1407

Objective C - how to append a number to the end of another number?

I'm wanting to append a number to the end of another number, example:

123 + 4

1234

Upvotes: 4

Views: 324

Answers (3)

thelaws
thelaws

Reputation: 8001

NSInteger a = 123;
NSInteger b = 4;
NSInteger c = [[NSString stringWithFormat:@"%ld%ld", (long)a, (long)b] integerValue];

Upvotes: 10

CRD
CRD

Reputation: 53000

If you want to do it all numerically then @bdares is the right direction, here is one option for the missing detail (typed at terminal):

NSInteger a = 123;
NSInteger b = 4;

NSInteger ab = a * (NSInteger)pow(10.0, ceil(log10(b+1))) + b;

pow, ceil & log10 are from math.h

Upvotes: 2

user684934
user684934

Reputation:

Get the length of the second number, k. Multiply first number by 10k. Add in second number.

Upvotes: 13

Related Questions