Jan 4, 2013

Converting CGFloat to int [Objective-C]

Posted Jan 4, 2013
There are cases that you want to convert float numbers to integer. One example is when you're using a frame's attribute, say, frame.size.width and you want to know if it's odd or even number. Before you can do a modulo operation, the numbers must be in integer format.

The easiest way is to round the float value first and typecast it to int.

int integerWidth = (int) roundf(myFrame.size.width);

Hope this helps!

No comments:

Post a Comment