2016-08-04 62 views
-2

我需要从分区中提取特定的整数部分。我想出了一个简单的函数,它返回我需要的东西。例如,如果我得到40,我需要整数,在划分到12后留下。所以,如果我有40我需要3(3 * 36 + 4 = 40)。如何从分区中获取整数

我试图NSLog(@"-40 modulo %i", (div(42, 12)));,它输出了我3

然而,试图一个NSInteger VAR分配给该输出错误:

_yearsModif = div(self.counterForYearsModif, 12); 

它输出红色警告 - Assigning to 'NSInteger' (aka 'long') from incompatible type 'div_t'

如何将结果赋值给简单的整数变量?谢谢。

+1

为什么这么复杂?如果你的数学是整数,你可以使用模运算符'%'?或者只是将它计算出来:'int x = 40; int div = x/12; int result = x - (div * 12);'。或者我错过了什么? – Legoless

回答

1

分割。整数部门已经做你想做的事情:

NSInteger counterForYearsModif = 40; 
NSInteger _yearsModif = counterForYearsModif/12; 
NSLog(@"%ld", _yearsModif); // 3 
+0

四舍五入么?如果我得到2.9并且它评估为3呢?那可能吗? –

+0

是的,当然。但这不是你问的。 – matt