Find   


An Affiliate of AIHorizon




This tip submitted by Gorn on 2005-10-24 07:32:31. It has been viewed 36822 times.
Rating of 3.8299 with 194 votes

Rounding to an Integer



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.
Example:
In --> Out
1.1 --> 1
1.5 --> 2
1.7 --> 2
1 --> 1

Feel free to email me with comments or suggestions. :)




More tips


Add a tip!



-----
Interested in advertising with us?
Please read our privacy policy.
Copyright © 1997-2005 Cprogramming.com. All rights reserved.