Im having issues with a block of code that involves rounding floats to the nearest upper x. What i'm trying to do is take whatever number i have and round it up to the next highest x. My code seems to work 80% of the time, but have recently found some cases where it is not returning a number that i would expect. For example, if i wanted it rounded to the next highest 500, the code below shows what i would have.
CGFloat number = 3780;
roundedNumber = 500 *ceilf(number/500 +.5);
Here i would expect the answer to return 4000. Instead, it returns 4,500.
Is there something about the math above that i am missing/not understanding? why is it only for some case this seems to "over round" my numbers? is there a simpler way to get what i'm after than this?
3780 /500 + 0.5 -> 7.56 + 0.5 -> 8.06 ceil(8.06) -> 9 9 * 500 -> 4500