How to safely create an NSInteger from an CGFloat?

cocoa-touch, iphone, uikit

Solution

You want the c function lrintf() which rounds a floating point to a long int.

Problem

I've got an CGFloat but need it as an NSInteger. The float value is like 2.0f, so I don't mind about fractional parts and loosing precision. What's a legal way to convert it into NSInteger without trouble (except the loss of precision, of course)? ``` NSInteger niceInt = niceCGFloat; ``` seems too simple, smells buggy. Maybe you can explain?

Original source