# How do I calculate the round of a number ?

I need to round a number .. there is no function in reference to do that. I have no idea how to do it. It will be nice if there was a library that I could include to add some more math functions. Maybe like these: abs, floor, int, mround (returns a number rounded to a specified multiple), roundup, rounddown, trunc

Thanks !

"round a number"

Up or down?

Have you tried the Math functions here: http://arduino.cc/en/Reference/HomePage

Math

min() max() abs() constrain() map() pow() sqrt()

You could do worse than use round():

``````#include <math.h>

float bar = 3.14159;
float foo = round(bar); // foo = 3.0 now
``````

http://www.nongnu.org/avr-libc/user-manual/group__avr__math.html#ga6eb04604d801054c5a2afe195d1dd75d

“The round() function rounds __x to the nearest integer, but rounds halfway cases away from zero (instead of to the nearest even integer).”

I guess abs helps to get to a rounded value. Sorry, I missed that.
Need to round down if <0.5 and up if >= 0.5. Pretty simple. Don’t really care if its <= and then >.
math.h sure does it ! Don’t know how I didn’t see that library. Will pay more attention next time.

Thanks !

bspin: Don't know how I didn't see that library.

I saw that library referenced once, but not linked to. It would be nice if that page had a clear download link. Or if it were in the Libraries section of the Arduino playground, under "math" that would be good too.

Or if it were in the Libraries section of the Arduino playground, under "math" that would be good too.

Why? It isn't an Arduino specific library.

example of 2-place round without library function, without bounds check

``````float val = 1.234;
val = float( long(val * 100)) / 100.0;
``````