Keil Logo

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: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.