Another option is to add 0.5 onto the number and then floor it, and then cast the result to an int.
if num is a double variable, then this would do it:
- int j = static_cast<int>(floor(num + 0.5));
I believe you'll need to include <cmath> to use the floor function.
this works because the floor function always rounds down, and any number that with a decimal of .5 or higher is raised to the next integer before being rounded. so that with 4.3 is increased to 4.8 and then floored giving you 4; while 4.8 would be raised to 5.3 which is floored to 5.
This is assuming that you want a decimal value of .5 to round up, if you want .5 to round down, then you can do this:
- int j = static_cast<int>(ceil(num - 0.5));
again needing to include <cmath> would be required.