Convert double to int

This is a discussion on Convert double to int within the C Programming forums, part of the General Programming Boards category; I need to know how to convert a number of the type double to a number of the type int. ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    Convert double to int

    I need to know how to convert a number of the type double to a number of the type int. My double numbers have a lot of digits behind the decimal point and I need to have these properly rounded(not a 'ceil' or 'floor' type of rounding) after the conversion has taken place. Is there a simple way to do this? What is the best way to do it? Below is some code that does not work.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        double x, y;
        int a, b;
        x = 3.322884; 
        y = 3.700219;
        a = x;
        b = y; 
        printf("a = %d, b = %d\n", a, b);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Add whatever your rounding point is to it, then just assign it to an integer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Something like this?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        double x, y;
        int a, b;
        x = 3.322884; 
        y = 3.700219;
        x += .5;
        y += .5;
        a = x;
        b = y; 
        printf("a = %d, b = %d\n", a, b);
    }

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ditto...

    mw
    Last edited by Lionmane; 08-25-2005 at 02:47 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's why we both said to add the rounding point and then assign to an int.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    Add .5 and round down. Thanks, it worked.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Add 0.5 and truncate, I might say. But do you also need to consider negative values?
    Code:
    #include <stdio.h>
    
    int foo(double value)
    {
       return value < 0 ? value - 0.5 : value + 0.5;
    }
    
    int bar(double value)
    {
       return value + 0.5;
    }
    
    int main()
    {
        double x = 3.322884;
        double y = 3.700219;
        printf("foo(%f) = %d, foo(%f) = %d\n", x, foo(x), y, foo(y));
        printf("bar(%f) = %d, bar(%f) = %d\n", x, bar(x), y, bar(y));
        x = -x;
        y = -y;
        printf("foo(%f) = %d, foo(%f) = %d\n", x, foo(x), y, foo(y));
        printf("bar(%f) = %d, bar(%f) = %d\n", x, bar(x), y, bar(y));
        return 0;
    }	
    
    /* my output
    foo(3.322884) = 3, foo(3.700219) = 4
    bar(3.322884) = 3, bar(3.700219) = 4
    foo(-3.322884) = -3, foo(-3.700219) = -4
    bar(-3.322884) = -2, bar(-3.700219) = -3
    */
    Then perhaps it might be: increase distance from zero by 0.5.
    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.*

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    thetinman,

    If your compiler supports C99, you can use lround(), it takes a double and returns a long.
    Last edited by n7yap; 08-25-2005 at 06:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21