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.

It would be nice if that page had a clear download link.

For math.h? You already have math.h. You don't need to download it.

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;

aefields: It would be nice if that page had a clear download link.

The Arduino environment is essentially a set of training wheels built around the more generic AVR-GCC and AVR-LIBC projects. When you compile a sketch, these core tools are used to compile and link. Pretty well everything documented in the AVR-LIBC documentation (www.nongnu.org/avr-libc/) is directly usable in Arduino and works as advertised there. You will find numerous links in these forums back to the AVR-LIBC documentation.

Despite the fact that the AVR-LIBC documentation is referenced on the Arduino References page, it is one of my pet peeves that it is not more prominently referenced, or more completely re-documented in the Arduino docs. If you look, you'll see: "Looking for something else? .... The Arduino language is based on C/C++. It links against AVR Libc and allows the use of any of its functions; see its user manual for details."

PaulS:

It would be nice if that page had a clear download link.

For math.h? You already have math.h. You don't need to download it.

Oh! I saw a sketch which had an include statement for math.h and that misled me. I thought it was an Arduino library and had to be in the "libraries" folder. Thanks for clarifying.