Joe
Joe

Reputation: 145

Rounding up to the nearest whole number

I've been searching for quite awhile and can't seem to find anything on this. How do I round a number up to the nearest whole number? I'm using the number of objects in an array and dividing it by 3. Say [array count] is 10, and I want to get a 4 as the result of 10/3. Or [array count] is 23, and I want to get an 8. How do I do this? Thanks in advance.

Upvotes: 4

Views: 5323

Answers (2)

PengOne
PengOne

Reputation: 48398

Be sure to cast the number you're rounding:

int roundedNumber = ceil((double)number/3);

Otherwise integer arithmetic will truncate.

Upvotes: 14

Hnatt
Hnatt

Reputation: 5935

ceil() function is what you are looking for.

Upvotes: 2

Related Questions