Thread: How to convert from float to int?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    25

    How to convert from float to int?

    Hi,
    this is a really simple problem, but I just can't find a solution.
    How do you convert a float like 12.34 to a rounded integer like 12??

    Thanks to whoever answers, your help is much appreciated

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    If you really want to round the number (i.e. 2.3->2 but 2.6->3) then you need to write your own function. e.g.:
    Code:
    #include <math.h>
    
    int Round(float myfloat)
    {
      double integral;
      float fraction = (float)modf(myfloat, &integral);
    
      if (fraction >= 0.5)
        integral += 1;
      if (fraction <= -0.5)
        integral -= 1;
    
      return (int)integral;
    }
    
    int main(void)
    {
      float myfloat;
      int myint;
      myfloat = 2.3f;
      myint = (int)myfloat;
      printf("%d\n",myint);
      myfloat = 7.6f;
      myint = (int)myfloat;
      printf("%d\n",myint);
      printf("%d\n", Round(2.3f));
      printf("%d\n", Round(7.6f));
      return 0;
    }
    I'm not sure if you strictly need all those casts, but my compiler gives me warnings if I don't include them, and I don't see the harm (maybe someone could tell me if there is a good reason for not explicitly stating casts - thanks!).
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    25

    thanks guys, very helpful

    thanks thats just what I needed,
    many thanks

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by DavT
    If you really want to round the number (i.e. 2.3->2 but 2.6->3) then you need to write your own function. e.g.:
    Code:
    #include <math.h>
    
    int Round(float myfloat)
    {
      double integral;
      float fraction = (float)modf(myfloat, &integral);
    
      if (fraction >= 0.5)
        integral += 1;
      if (fraction <= -0.5)
        integral -= 1;
    
      return (int)integral;
    }
    That would work, but this is much faster (no conditionals to slow you down):

    Code:
    int Round(float myfloat)
    {
      return (int)(myFloat + 0.5);
    }
    Works because x rounded to the nearest int is the same as the integer part of x + 0.5.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    13

    Question

    Isn't there a round function in C99?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Isn't there a round function in C99?

    Yes.
    7.12.9.6 The round functions

    Synopsis

    1
    #include <math.h>
    double round(double x);
    float roundf(float x);
    long double roundl(long double x);


    Description

    2 The
    round functions round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction.

    Returns

    3 The
    round functions return the rounded integer value.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Originally posted by DavT
    I'm not sure if you strictly need all those casts, but my compiler gives me warnings if I don't include them, and I don't see the harm (maybe someone could tell me if there is a good reason for not explicitly stating casts - thanks!).
    The only reason not to cast is because it can suppress more warnings than you meant to suppress. I wish I remembered the details/instances of it happening so I could tell you, but I don't.
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM