Evgeniy Kleban Evgeniy Kleban - 2 months ago 6
Objective-C Question

How to get integer from division

I need to extract specific integer part from division. I figured out a simple function that return what i need. For example, if i got 40, i need at integer that left after division to 12. So, if i got 40 i need 3 (3*36+4 = 40).

I tried

NSLog(@"-40 modulo %i", (div(42, 12)));
and it output me 3.

However, trying to assign an NSInteger var to this output an error:

_yearsModif = div(self.counterForYearsModif, 12);

It output red warning -
Assigning to 'NSInteger' (aka 'long') from incompatible type 'div_t'

How to assign result to simple integer variable? Thanks.


Just divide. Integer division already does what you want:

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