CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How do I convert float to Int or long?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cchappyboy



Joined: 03 Dec 2008
Posts: 45

View user's profile Send private message

How do I convert float to Int or long?
PostPosted: Tue Nov 24, 2009 11:51 am     Reply with quote

Is there any way to convert float to int or long in ccsc?
Any concern to be appreciate in advance.
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Tue Nov 24, 2009 4:33 pm     Reply with quote

It is a standard C language, so:
Code:

int myval;
float test;

test=20.5;
myval=(int)test;
Piccolo



Joined: 19 May 2006
Posts: 23
Location: Connecticut

View user's profile Send private message

PostPosted: Tue Nov 24, 2009 5:33 pm     Reply with quote

Note: when you cast from a float to an integer your integer will "drop off" any of the original float value that was to the right of the decimal point. This means 20.5 will become 20 as an integer. Also, 20.9 will become 20, so make sure if you are converting from a float to an integer that you really don't need the decimal portion...
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Wed Nov 25, 2009 7:54 am     Reply with quote

Depending on your needs, you may want to round up when doing this, e.g.

myval = (int) (test + 0.5);

But, consider the impact when test is negative!

Ken
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group