We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi all, I like to round a float to the nearest integer value and assign the value to an integer. How do I do that in C code? In know it is something with "round" but I can not find the proper syntacs. Does someone know? Regards, Erik
Add 0.5 to the float variable and then cast it to an integer ?
"Add 0.5 to the float variable and then cast it to an integer" Or if the float is negative, add -0.5.
Yes, I see now. I don't know why I did not see this before. Anyway, thank you.... Regards Erik
See also ceil(), floor() in math.h. float.h has a FLT_ROUNDS #define that tells you which way the system rounds floating point (up, down, toward zero, nearest, etc).