Shop OBEX P1 Docs P2 Docs Learn Events
C converting a float to an int — Parallax Forums

C converting a float to an int

cmeehancmeehan Posts: 8
edited 2013-10-02 19:33 in General Discussion
Hi All,

I have written some C code using floats and I need to use a function that wants ints passed to it. How do convert a float to an int? I only care about the whole number part, if that matters.

Thanks
Chris

Comments

  • SapphireSapphire Posts: 496
    edited 2013-09-30 16:08
    Use a cast.
    float f;
    function(int);
    
    function((int)f);  /* call function() with f as an int */
    
  • RossHRossH Posts: 5,462
    edited 2013-09-30 16:11
    The most trivial answer is to use type casting:
       int i;
       float f;
    
       i = (int) f;
    

    But the general case gets a bit more complex than that. For a summary, check out http://www.cs.tut.fi/~jkorpela/round.html

    R
    oss.
  • cmeehancmeehan Posts: 8
    edited 2013-10-01 15:22
    That worked.

    Thanks
    Chris

    PS - How do I mark a thread as "Solved"
  • Invent-O-DocInvent-O-Doc Posts: 768
    edited 2013-10-02 07:45
    Can I likewise assume the reverse, casting an int to a float?

    int i;
    float f;

    f = (float) i;

    I tried the above in C++/Simple IDE to convert an int to a double, and the double remained 0.00000 no matter what I did.
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-10-02 08:14
    It works for me in C. Can you post the code that's failing?
  • Invent-O-DocInvent-O-Doc Posts: 768
    edited 2013-10-02 09:31
    I'm not on my computer. I'll try it out tonight and let you know if it works, or not.
  • davejamesdavejames Posts: 4,047
    edited 2013-10-02 09:40
    cmeehan wrote: »
    PS - How do I mark a thread as "Solved"

    Hi cmeehan,

    Edit your original post in advanced mode, and change the prefix box to "solved".

    Regards,

    DJ
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-10-02 11:31
    C++ generally has more protection against errors that might cause trouble.

    While it seems to me that this should almost be done with a Rounding function, that may well not be enough to get C to see the number as an integer. This approach is actually a Rounding Down.
    Going the other way might have been considered more hazardous as the computer depends heavily on integer to all sorts of representations of binary code. So the gnomes that made C++ might just have created an additional 'no-no' that requires some sort of override.

    So far I have ignored C++ as it seems to add a lot of complexity.
  • RossHRossH Posts: 5,462
    edited 2013-10-02 19:33
    For both C and C++, casts work both ways.

    For example, the following code should print i = 999, f = 999.000000 on any C or C++ compiler:
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
    
       int i = 0;
       float f = 999.0;
    
       i = (int) f;
       f = (float) i;
         printf("i = %d, f = %f\n", i, f);
    
       return 0;
    
    } 
    
Sign In or Register to comment.