Rounding a float to the nearest int
-
How can I round a float to an integer? If I do int i=(int)3.0f it's not good because if 3.0f is like 2.9999999 in memory, I'll get i=2, which is unacceptable. Math.Round only accepts doubles/decimals, which is not an option because I need a float. Any idea?
-
How can I round a float to an integer? If I do int i=(int)3.0f it's not good because if 3.0f is like 2.9999999 in memory, I'll get i=2, which is unacceptable. Math.Round only accepts doubles/decimals, which is not an option because I need a float. Any idea?
Tony_P wrote:
If I do int i=(int)3.0f it's not good because if 3.0f is like 2.9999999
Have you tried it?
Tony_P wrote:
Math.Round only accepts doubles/decimals, which is not an option because I need a float
So what? Casting a single to a double is lossless. I think you're concerned about non-existent problems.