Well, you already use casting, and sometimes it can give you exactly what you need. If you just add 0.5 to your number and then produce an integer, you get a rounded value.
13.4 + 0.5 = 13.9 -> 13 (when casted to int)
13.49 + 0.5 = 13.99 -> 13
13.5 + 0.5 = 14.0 -> 14
13.6 + 0.5 = 14.1 -> 14
Here is how you could write a method.
int Roundoff(float num)
{
return (int) (num + 0.5);
}
source
share