Rounding functions
Appearance
Rounding Functions
To round a fractional number to the nearest integer (in C):
int round(double d) { return (int)(d + 0.5); }
The above, however, rounds negative numbers unintuitively, e.g., -2.7 becomes -2 instead of -3. At the expense of more computation, one can use:
int sround(double d) { return (int)(d + (d >= 0? 0.5 : -0.5)); }