Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide int's and round up in Objective-C

Tags:

objective-c

I have 2 int's. How do I divide one by the other and then round up afterwards?

like image 623
Slee Avatar asked Feb 07 '11 20:02

Slee


People also ask

Does division round up or down in C?

When doing an integer division in c++, the result will always be rounded down, so your rounding system isn't off :) For example even if you divide 1999 by 1000, the result will be 1 if both 1999 and 1000 are integers.

How do you divide integers by rounding up?

If the divisor and dividend have the same sign then the result is zero or positive. If the divisor and dividend have opposite signs then the result is zero or negative. If the division is inexact then the quotient is rounded up.

Does INT division round down or up?

Yes, the result is always truncated towards zero. It will round towards the smallest absolute value.


1 Answers

If your ints are A and B and you want to have ceil(A/B) just calculate (A+B-1)/B.

like image 148
Howard Avatar answered Sep 29 '22 21:09

Howard