I often need to floor or ceil a CGFloat
to an int
, for calculation of an array index.
The problem I permanently see with floorf(theCGFloa
EDIT - read the comments for reasons why this answer isn't right :)
Casting a float
to an int
is an implicit floorf
i.e. (int)5.9
is 5
. If you don't mind that then just cast :)
If you want to round up then just cast the result of a ceilf
- casting after rounding shouldn't introduce any errors at all (or, if you want, add one before casting i.e. `(int)(5.9+1)' is '6' - same as rounding up).
To round to the nearest, just add 0.5 - (int)(5.9+0.5)
is 6
but (int)(5.4+0.5)
is 5
. Though I would just use roundf(5.9)
:)