How to Rounding to an Integer?

0 votes
asked Dec 19, 2013 in C++ by Victor

1 Answer

0 votes
answered Dec 19, 2013 by admin (8,135 points)


  Many people find the problem of their float points to integers, it always rounds down. A good way to use the round down, against itself, you can create code that causes it to go up, then round down, creating a rounding up OR down effect. 

The code I've always seen and used is: 

Integer = Float + .5 
Note: This works for almost all programming languages 

This will add .5 to the float number, and it will round down to be either the number above the original Float, or the number below. 
In --> Out 
1.1 --> 1 
1.5 --> 2 
1.7 --> 2 
1 --> 1