Objective C - how to append a number to the end of another number?
I'm wanting to append a number to the end of anoth开发者_JAVA技巧er number, example:
123 + 4
1234
Get the length of the second number, k
. Multiply first number by 10k. Add in second number.
NSInteger a = 123;
NSInteger b = 4;
NSInteger c = [[NSString stringWithFormat:@"%ld%ld", (long)a, (long)b] integerValue];
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
精彩评论