doubles/printf & rounding

Discussion in 'C++' started by wombat, Sep 5, 2007.

  1. wombat

    wombat Guest

    Is there a way to prevent rounding when using printf for print a
    double/float?

    Example:
    double x=3.51;
    printf("%.0f\n", x);



    The output of this is 4, when I want it to be 3.

    I've been surfing the net but I've been coming up empty. Big thanks for
    any help.
    wombat, Sep 5, 2007
    #1
    1. Advertising

  2. wombat

    Jim Langston Guest

    "wombat" <> wrote in message
    news:-sjc.supernews.net...
    > Is there a way to prevent rounding when using printf for print a
    > double/float?
    >
    > Example:
    > double x=3.51;
    > printf("%.0f\n", x);
    >
    >
    >
    > The output of this is 4, when I want it to be 3.
    >
    > I've been surfing the net but I've been coming up empty. Big thanks for
    > any help.


    printf("%.0f\n", x - 0.5);

    This rounds down, but doesn't work correctly for negative x, in which case
    you would want to add 0.5 (-3.51 - 0.5 = -4.01 so would be output as -4
    instead of -3 as wanted).
    Jim Langston, Sep 5, 2007
    #2
    1. Advertising

  3. wombat

    James Kanze Guest

    On Sep 5, 5:22 am, wombat <> wrote:
    > Is there a way to prevent rounding when using printf for print a
    > double/float?


    > Example:
    > double x=3.51;
    > printf("%.0f\n", x);


    > The output of this is 4, when I want it to be 3.


    > I've been surfing the net but I've been coming up empty. Big thanks for
    > any help.


    Nothing to do with printf: the same rules apply with the cleaner
    iostreams. You can use floor() or ciel() to control how you
    want rounded; C99 (and the next version of C++) add some more
    (round() and trunc(), for example), which may already be
    available with your compiler(s).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 5, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dan
    Replies:
    1
    Views:
    2,313
    Jack Klein
    Nov 26, 2003
  2. Yogesh Khanolkar

    incorrect printf o/p for doubles

    Yogesh Khanolkar, Oct 30, 2003, in forum: C Programming
    Replies:
    13
    Views:
    550
    Dan Pop
    Nov 3, 2003
  3. itportal

    Rounding floats/doubles

    itportal, Jan 16, 2006, in forum: C Programming
    Replies:
    9
    Views:
    510
    Gordon Burditt
    Jan 17, 2006
  4. Allin Cottrell

    printf on doubles

    Allin Cottrell, Apr 5, 2007, in forum: C Programming
    Replies:
    6
    Views:
    359
    Allin Cottrell
    Apr 6, 2007
  5. KyoGaSuki

    Rounding doubles

    KyoGaSuki, Feb 12, 2008, in forum: Java
    Replies:
    6
    Views:
    532
    Arne Vajhøj
    Feb 13, 2008
Loading...

Share This Page