View previous topic :: View next topic |
Author |
Message |
dynamitron
Joined: 18 Mar 2009 Posts: 38
|
conversion float to int8 |
Posted: Tue Jun 09, 2009 4:31 pm |
|
|
I am developing a thermostat based on a DS18b20.
I would like to compared the temperature stored in a float variable to an integer 8 bit.
For this purpose I would like to convert the float variable into a int8 and then make the comparison ?
How to do this conversion assuming that I am not interested in the decimal part of the float ?
Thanks in advance for the hints !! ,
Dynamitron |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
nostep
Joined: 04 Mar 2009 Posts: 16
|
|
Posted: Wed Jun 10, 2009 5:05 am |
|
|
You don't necessarily have to convert it then compare it. You could do this:
Code: |
// declare variables
int num_int;
float num_float;
// Get your data into the variables as you need to
num_int=127;
number_float=124.3;
// Here's the comparison using a "cast" method
if(num_int > (int)num_float) // do what you need to do now |
|
|
|
dynamitron
Joined: 18 Mar 2009 Posts: 38
|
|
Posted: Wed Jun 10, 2009 1:57 pm |
|
|
Thanks to both of you, this is exactly what I wanted.
I like the "no conversion" comparison.
Dynamitron |
|
|
Ttelmah Guest
|
|
Posted: Wed Jun 10, 2009 2:11 pm |
|
|
If you had looked at the link PCM programmer gave, you would see exactly the same suggestion. This _is_ a cast, and what you are doing, is converting the float to an int, before the conversion.....
Best Wishes |
|
|
|