开发者

Rounding up to the nearest whole number

开发者 https://www.devze.com 2023-03-21 13:16 出处:网络
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

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, an开发者_如何学God I want to get an 8. How do I do this? Thanks in advance.


Be sure to cast the number you're rounding:

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

Otherwise integer arithmetic will truncate.


ceil() function is what you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消