# Round off float to integer

This is a discussion on Round off float to integer within the C Programming forums, part of the General Programming Boards category; Hi Guys, This isn't homework, it's for a personal project I have a float values I'd like to round off ...

1. ## Round off float to integer

Hi Guys,
This isn't homework, it's for a personal project

I have a float values I'd like to round off to the nearest integer value.
That is to say, if the float value is 44.234533, the integer value should
be 44. If the float value is 44.682101, the integer value should be 45.

How do I do this?
Thanks, Art.

2. maybe it helps..

ceil(x) <math.h> Returns the smallest integral
value that is not less than x :
if x is 45.23 , ceil(x) is 46.0

floor(x) <math.h> Returns the largest integral value
that is not greater than x :
if x is 45.23 , floor(x) is 45.0

3. Here's a neat little trick:

Code:
```round_to_nearest_int( double x ) {
return floor( x + 0.5 );
}```
Can you figure out for yourself why this works?

4. No, I can't see it, but I did figure out this much:

Code:
```float x = 44.56;
float w;

w = x - floor(x);
if (w < 0.5) {// round down} else {// round up};```

5. Yes I can see why now, it's the same thing