开发者

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

开发者 https://www.devze.com 2023-03-25 10:50 出处:网络
I\'m wanting to append a number to the end of anoth开发者_JAVA技巧er number, example: 123 + 4 1234Get the length of the second number, k.Multiply first number by 10k.Add in second number.NSInteger a

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

0

精彩评论

暂无评论...
验证码 换一张
取 消