Keil™, An ARM® Company

Technical Support

GENERAL: CONVERTING FLOATING-POINT NUMBER TO INTEGER


Information in this article applies to:

  • C166 All Versions
  • C251 All Versions
  • C51 All Versions

QUESTION

I would like to convert a floating-point number to an integer. Is there a standard math library routine to accomplish this?

ANSWER

No. The C Programming Language does not require library routines to perform simple type conversions. Types are converted using implicit type casting. For example:

long l;
float f;

l = f;      // f is converted to a long value
            // the result is stored in l

In the above example, the value of the floating-point variable f is converted to a long value and is assigned to l. If the value of f is outside the range of values for a long type, the maximum positive or negative long value is assigned to l. The value ranges for the following integer and floating point types are shown below;

Data Type        Bits  Bytes    Value Range
--------------------------------------------------------------
signed int       16    2        -32768 to 32767
unsigned int     16    2        0 to 65535
signed long      32    4        -2147483648 to +2147483647
unsigned long    32    4        0 to 4294967295
float (IEEE-754) 32    4        ±1.175494E-38 to ±3.402823E+38

MORE INFORMATION

SEE ALSO

Last Reviewed: Friday, July 15, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure