get the integer representation value of a float type variable in C

前端 未结 6 1932
悲哀的现实
悲哀的现实 2020-12-21 13:19

I have the number 20 (0x14) stored in a 32-bit register. The register is allocated to a C float variable representing the value 2.8e-44. Now I want to get the hexadecimal re

6条回答
  •  -上瘾入骨i
    2020-12-21 13:46

    You can directly cast it to integer as;

    float a = 7.4;
    int b = a; // this will be rounded to 7 and you will lose information
    

    Or you can use some built-int functions like round, ceil, floor etc.

    For reference: http://www.cplusplus.com/reference/cmath/round/?kw=round

提交回复
热议问题