Peter

Peter

  • NA
  • 14
  • 26.1k

How to "round" a float to an int?

Jan 4 2011 11:26 PM

Hi
how do I round a float to an int, such that if the decimal part is .5 or more, then the value is rounded up, otherwise the value is rounded down?
For example,  3.2f => 3; 3.5f => 4; 3.7f => 4; 4.2f => 4; 4.5f => 5; 4.7f => 5
If I use (int)Math.Round(3.5f), the result is 4 (ie what I want); whereas with (int)Math.Round(4.5f), the result is 4 (ie not what I want).
Is there a "Math" method which does rounding the way I want it?
Thanks,
Peter

Answers (3)