Let's specify the desired behavior for a function that rounds to the nearest integer. We'll call that function IntRound().
IntRound(4.2) = 4
IntRound(4.8) = 5
IntRound(4.5) = 4 or 5 (we don't really care which)
IntRound(4.2) = 4
IntRound(4.8) = 5
IntRound(4.5) = 4 or 5 (we don't really care which)
The <math.h> library in C (and the <cmath> library in C++) provide two functions that might be useful:
double ceil(double x) = smallest integer no less than x
double floor(double x) = largest integer no greater than xceil(4.2) = 5 ....... floor(4.2) = 4
ceil(4.8) = 5 ....... floor(4.8) = 4
ceil(4.5) = 5 ....... floor(4.5) = 4
ceil(4.2) = 4 ..... floor(4.2) = 5
ceil(4.8) = 4 ..... floor(4.8) = 5
ceil(4.5) = 4 ..... floor(4.5) = 5
For nonnegative x, floor(x) works like a truncation function  it discards the fractional part and returns only the integer part. For the rounding function you want to truncate [round down] if the fractional part is less than 0.5 and round up if it is more than 0.5. Consider this first draft of IntRound for nonnegative input:
 int IntRound(double x) {

return (int) floor(x + 0.5);

}
 ... or ...

int IntRound(double x) {

return (int) ceil(x  0.5);

}
What about when the input is negative? It still works!
IntRound(4.2) = floor(4.7) = 4
IntRound(4.8) = floor(5.3) = 5
IntRound(4.5) = floor(5.0 +/ epsilon) = 4 or 5
IntRound(4.2) = floor(3.8) = 4
IntRound(4.8) = floor(4.3) = 5
IntRound(4.5) = floor(4.0 +/ epsilon) = 4 or 5
(The "+/ epsilon" is due to the fact that floating point arithmetic can introduce a very small error, here called epsilon. This inaccuracy is present in all the cases, but it is only relevant to the outcome of floor in two of them.)
Scruggsy suggested casting a double into an int because doing so commonly has the result as the floor() function (at least for positive inputs). For instance:
 int IntRound(double x) {

return (int) (x + 0.5);

}
However, the C Standard makes no such guarantee. Depending on your compiler, casting a double to an int might ...
> be the same as the floor function
> be the same as the ceil function
> be the same as the IntRound function
> always round towards zero
> or do something else
Relying on implementationdependent behavior will get you in trouble sooner or later.
Suppose you have a double whose value is a price in dollars and you want to round to the nearest penny. You can accomplish this with IntRound too:
 double roundedPrice = (double) IntRound(price * 100.0) / 100.0;