double with no decimal ?

This is a discussion on double with no decimal ? within the C Programming forums, part of the General Programming Boards category; hey, just wondering if anyone could please tell me why this will not print a decimal value for d2: Code: ...

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    double with no decimal ?

    hey, just wondering if anyone could please tell me why this will not print a decimal value for d2:

    Code:
             i = (int)2 * p;
    	f = 2 * p;
    	d2 = 2 * p;
    
    	printf("i (2*pi)\t%d\t\t%f\t%d\n", i, f, d2);
    I've tried switching 2 to 2.000000000 but it makes no difference. for some reason it refuses to print a value with a decimal point for the value of d2 (which is declared as a double) . a value printed of 6.000000000 is what I'm looking for here.

    p is declared as a double and assigned 3.140000000, since the math.h header in my MSVC++ 6 doesn't seem to define a constant for pi (it says its M_PI on msdn, but when I use it I get "undeclared identifier" for M_PI, and yes math.h is included ).

    yes, I realize there will be truncation here, but it is not important for this program.


    thanks in advance!
    Last edited by Bleech; 09-11-2006 at 04:14 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You're using %d together with d2. Change it to %f.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    thanks OnionKnight for the fast reply.

    this seems to do the job:

    Code:
    printf("i (2*pi)\t%d\t\t%f\t%.9f\n", i, f, d2);
    I did try printing it as a float before I posted this, but I was only getting 5 digits after the decimal, but when I use .9 to set the precision it works fine .

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 09:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM

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