C code to round numbers

Posted by webgenius on Stack Overflow See other posts from Stack Overflow or by webgenius
Published on 2010-04-03T10:23:02Z Indexed on 2010/04/03 10:33 UTC
Read the original article Hit count: 172

Filed under:
|
|

Is there any way to round numbers in C?

I do not want to use ceil and floor. Is there any other alternative?

I came across this code snippet when I Googled for the answer:

(int)(num < 0 ? (num - 0.5) : (num + 0.5))

The above line always prints the value as 4 even when float num =4.9.

Please suggest a solution.

© Stack Overflow or respective owner

Related posts about c

    Related posts about rounding