Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 743
Joe Avatar asked Dec 04 '22 21:12

Joe


2 Answers

Be sure to cast the number you're rounding:

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

Otherwise integer arithmetic will truncate.

like image 189
PengOne Avatar answered Dec 23 '22 08:12

PengOne


ceil() function is what you are looking for.

like image 31
Hnatt Avatar answered Dec 23 '22 10:12

Hnatt