Objective-C: divide two integers and return a rounded integer value

How can I split two NSIntegers, like 13/4, and round the result to the next integer = 3?

I saw several examples converting integers to float and back to integer. But what is the recommended way with the least code for this?

+4
source share
1 answer

Assuming x >= 0 and y > 0 :

If you want to round: x / y

If you want to round: (x + y - 1) / y

If you want to round to the nearest: (x + y / 2) / y

+15
source

Source: https://habr.com/ru/post/1442846/


All Articles