View previous topic :: View next topic |
Author |
Message |
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
How do I convert float to Int or long? |
Posted: Tue Nov 24, 2009 11:51 am |
|
|
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
|
|
Posted: Tue Nov 24, 2009 4:33 pm |
|
|
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
|
|
Posted: Tue Nov 24, 2009 5:33 pm |
|
|
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
|
|
Posted: Wed Nov 25, 2009 7:54 am |
|
|
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 |
|
|
|