Float to byte help needed

Hi,

I am new to this forum and to the Arduino. I need to convert a float to a int. Only the whole number is needed, not the decimal part and the numbers will always be less than 150 dec.

Any help would be great.

Ray

try
(int)variable
or int(variable)

both should work.

You may want to add a half before you truncate.

What is the value of your float ? minimum, maximum? approx?
as an int is between -32768 and 32767

if its bigger you need a long

i have been using this to round off my float instead of typecasting as others have suggested and it works great.. and more accurate..

.
#define roundf(x) (int)(x*100+0.5)/100.0

i have been using this to round off my float instead of typecasting as others have suggested and it works great.. and more accurate..

#define roundf(x) [glow](int)[/glow](x*100+0.5)/100.0

Without casting, huh?

Without casting, huh?

;D

And while we're at it, your code produces garbage in the call roundf(a +1). Please stay away from #define, use inline functions and const declarations. People using #defines instead of those usually just produce substandard code which create more problems than they solve.

Furthermore, the gimmick with multiplying and dividing by 100 is just stupid. Add 0.005 instead.

Korman

[...]Please stay away from #define, use inline functions and const declarations.[...]

Amen!

... Add 0.005 instead.

That's only a solution for half of the problem. So then what is the missing half?