C convert floating point to int

199,464

Solution 1

my_var = (int)my_var;

As simple as that. Basically you don't need it if the variable is int.

Solution 2

Use in C

int C = var_in_float;

They will convert implicit

Solution 3

If you want to round it to lower, just cast it.

float my_float = 42.8f;
int my_int;
my_int = (int)my_float;          // => my_int=42

For other purpose, if you want to round it to nearest, you can make a little function or a define like this:

#define FLOAT_TO_INT(x) ((x)>=0?(int)((x)+0.5):(int)((x)-0.5))

float my_float = 42.8f;
int my_int;
my_int = FLOAT_TO_INT(my_float); // => my_int=43

Be careful, ideally you should verify float is between INT_MIN and INT_MAX before casting it.

Solution 4

double a = 100.3;
printf("%f %d\n", a, (int)(a* 10.0));

Output Cygwin 100.3 1003
Output MinGW: 100.3 1002

Using (int) to convert double to int seems not to be fail-safe

You can find more about that here: Convert double to int?

Share:
199,464
A. O.
Author by

A. O.

Just a random South-American girl passing by.

Updated on July 12, 2022

Comments

  • A. O.
    A. O. almost 2 years

    I'm using C (not C++).

    I need to convert a float number into an int. I do not want to round to the the nearest number, I simply want to eliminate what is after the integer part. Something like

    4.9 -> 4.9 -> 4
  • A. O.
    A. O. almost 10 years
    Really thanks! It worked! Sorry if the question was too simple, i am a newbie in c...
  • Jakub
    Jakub almost 10 years
    Make sure you do what you want/should for negative and large numbers. Things may be more complicated. If the floats are positive and smaller than the maximum integer on your platform, you ara safe.
  • saolof
    saolof over 4 years
    This is nice, but it is also undefined behaviour.
  • DownloadPizza
    DownloadPizza almost 4 years
    Using Macros with logic is not incredibly smart, as people tend to view them as functions. For example if you were to FLOAT_TO_INT(rand()) it would behave weird, better to make it a function
  • phuclv
    phuclv over 3 years
    pow returns a double, that's why you can't print it using %d. And using pow for that purpose is wrong because it may return incorrect results for integers. Use 1e4 instead to get 10^4. See Why does gcc compiler output pow(10,2) as 99 not 100?, Why pow(10,5) = 9,999 in C++, Why does pow(5,2) become 24?
  • user3629249
    user3629249 over 3 years
    regarding: #define FLOAT_TO_INT(x) ((x)>=0?(int)((x)+0.5):(int)((x)-0.5)) Those literals 0.5 is a double You really want float. Suggest: `#define FLOAT_TO_INT(x) ((x)>=0.0f?(int)((x)+0.5f):(int)((x)-0.5f))
  • user3629249
    user3629249 over 3 years
    unfortunately, the question is about converting float to int, not double to int
  • chux - Reinstate Monica
    chux - Reinstate Monica over 3 years
    " round it to lower" is incorrect/unclear for negative values. (int) truncates the fraction.
  • chux - Reinstate Monica
    chux - Reinstate Monica over 3 years
    @user3629249 (x)+0.5f can give incorrect rounded results when the sum is inexact. (x)+0.5 avoids this by using wider double math. IAC lroundf(), roundf() is better.
  • chux - Reinstate Monica
    chux - Reinstate Monica over 3 years
    @user3629249 Same issue applies with float, yet a float only example would be better here.
  • Tomi Ollila
    Tomi Ollila over 3 years
    OOh, this (int)14e was great. Is it safe? 1e4 w/o casting has type 'double' (as the pow() case)
  • Tomi Ollila
    Tomi Ollila over 3 years
    Lost change to edit above comment more -- just recently I had use case for 1e6, but (as usual) I used printf("%d\n", 1e6); to test it out and got weird results. if const int million = 1e6 is found safe I start using such a format.
  • chux - Reinstate Monica
    chux - Reinstate Monica over 3 years
    1e6 relies on the compiler to make a quality double to int conversion. Less likely a problem than pow(), yet I see no C spec guaranteeing the best answer. Alternatives to (int)1e6: Why write 1,000,000,000 as 1000*1000*1000 in C? and why not safe
  • Tomi Ollila
    Tomi Ollila over 3 years
    yes pow() is bad, compared to 10e4 in this example (have to fix, with edit commit). I wondered why that 10e4 is double not int, then tried 1.12e1 and 12e-3 to understand.
  • Viraj Mohite
    Viraj Mohite about 2 years
    Simple and short