CCS does not monitor this forum on a regular basis. Please do not post bug Reports on this forum. Send them to support@ccsinfo.com
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: 20268

 Posted: Tue Jun 09, 2009 4:58 pm Do it by casting the float to an int. C tutorial page: http://www.cs.cf.ac.uk/Dave/C/node9.html#SECTION00930000000000000000
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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 6 Hours Page 1 of 1

 Jump to: Select a forum Software----------------General CCS C DiscussionCode LibraryEZ App Lynx Hardware----------------CCS ICD / Mach X / Load-n-Go
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum