xcode 如何在Objective-C中连接两个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1993965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to concatenate two numbers in Objective-C
提问by Flocked
How can I concatenate two numbers like 7 and 6 to result in the number 76, or 3 and 3 so the result is 33, in objective-c?
在objective-c 中,如何将两个数字(如 7 和 6)连接起来得到数字 76,或者 3 和 3,所以结果是 33?
回答by Mike
There is no built in symbol to concatenate numbers. However, you can accomplish this by doing:
没有内置符号来连接数字。但是,您可以通过执行以下操作来完成此操作:
int first; /* Assuming this is initialized to the first number */
int second; /* Assuming this is initalized to the second number */
int myVal = [[NSString stringWithFormat:@"%d%d",first, second] intValue];
回答by Matt
FirstNum * 10 + secondNum :-)
FirstNum * 10 + secondNum :-)
回答by ceejayoz
That's not a numeric operation, it's string concatenation.
这不是数字运算,而是字符串连接。
回答by jkeesh
If you want two numbers x and y to add to xy, you can do
如果你想要两个数字 x 和 y 添加到 xy,你可以这样做
10*x + y.
10*x + y。
For 7 and 6
对于 7 和 6
7*10 + 6 = 76
7*10 + 6 = 76
回答by acerb
I don't know much about objective-c but I would say:
我对objective-c了解不多,但我会说:
If you get the numbers from an array, like nums= array(7,6), initialize result= 0 and then do a foreach on them. For each value you find, do : res= res*10 + value. At the end, even if you got 7 numbers to concatenate you'll get the result right. ie:
Array nums= Array(7,6,8,9); int res= 0; int value; foreach (value in nums) res= res*10 + value;
If you can use strings, just concatenate them like suggested above. there is probably a function to concatenate all values from an array as well to make it flexible.
如果您从数组中获取数字,例如 nums= array(7,6),则初始化 result= 0,然后对它们执行 foreach。对于您找到的每个值,请执行:res= res*10 + value。最后,即使您有 7 个数字要连接,您也会得到正确的结果。IE:
数组 nums= Array(7,6,8,9); 内部资源= 0; 整数值;foreach (数值以 nums 为单位) res= res*10 + value;
如果您可以使用字符串,只需按照上面的建议将它们连接起来。可能还有一个函数可以连接数组中的所有值以使其灵活。
Hope it helps
希望能帮助到你
C^
^