Rounding to an IntegerThis tip submitted by Gorn on 2005-10-24 07:32:31. It has been viewed 96977 times.
Rating of 5.8 with 634 votes
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
Feel free to email me with comments or suggestions. :)
Help your fellow programmers! Add a tip!