How to convert a float to an Int by rounding to the nearest whole integer

type-conversion

Solution

Actually Paul Beckingham's answer isn't quite correct. If you try a negative number like -1.51, you get -1 instead of -2.

The functions round(), roundf(), lround(), and lroundf() from math.h work for negative numbers too.

Problem

Is there a way to convert a `float` to an `Int` by rounding to the nearest possible whole integer?

Original source