1

like 52.7-->53, 5.5-->6, 3.2->3

Emil
  • 93
  • 2
  • 11

2 Answers2

0

To handle negative numbers properly (-52.7 ==> -53 (away from zero)), you must check if the initial value is negative:

((int)(num + ((num > 0)? +0.5 : -0.5)))
abelenky
  • 63,815
  • 23
  • 109
  • 159
-2
int round(float num) {
  return (int)(num + 0.5);
}

Edit: This will not work for large value of float and negative numbers.
Edit Again: This was the simple idea of how rounding works, but after reading comment on question this thread : Concise way to implement round() in C? has everything explained for a proper implementation of round() function

Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • 2
    Note that this rounding mode is *round upwards*, not *round away from zero*, which might not be what someone expects. Also, it won't work for large values which `int` cannot represent. – user694733 Mar 31 '17 at 06:45
  • 1
    You need to check double to int conversion, since possible loss of data – avatli Mar 31 '17 at 06:46