Printf Question

Discussion in 'C++' started by Mandragon03@gmail.com, Apr 16, 2007.

  1. Guest

    I am looking for a way to take a floating point number and get rid of
    any extraneous 0's at the end. For instance if I have

    myFloat = 9999;

    printf("%f", myFloat);

    I get 9999.0000. I want to get rid of these 0's.

    Here is one catch. I also need to be able to do this:

    myFloat = .531;

    printf("%f", myFloat);

    I get .5310000

    I need to get .531.

    I realize I can do this

    printf("%2f", myFloat);

    which will limit the mantissa to two places, but this will not work
    for what i am doing because I can not lose that much precision on
    floats like .05369.

    Thank you for you time,

    Mandragon03
    , Apr 16, 2007
    #1
    1. Advertising

  2. wrote:
    > I am looking for a way to take a floating point number and get rid of
    > any extraneous 0's at the end. For instance if I have
    >
    > myFloat = 9999;
    >
    > printf("%f", myFloat);
    >
    > I get 9999.0000. I want to get rid of these 0's.
    >
    > Here is one catch. I also need to be able to do this:
    >
    > myFloat = .531;
    >
    > printf("%f", myFloat);
    >
    > I get .5310000
    >
    > I need to get .531.
    >
    > I realize I can do this
    >
    > printf("%2f", myFloat);
    >
    > which will limit the mantissa to two places, but this will not work
    > for what i am doing because I can not lose that much precision on
    > floats like .05369.


    The problem actually is rather simple. The default is 7 decimal
    digits in the result, which means that 0.5310000 and 0.530999951
    will give you the same output. The zeros are there to indicate to
    what digit the output was _rounded_ to.

    If you really need to lose the trailing zeros, you will have to
    trim them yourself. Convert your number to a string and drop all
    chars from it until the first non-zero:

    std::eek:stringstream s;
    s << myFloat;
    std::string str = s.str();
    while (str.size() > 1 && *str.rbegin() == '0')
    str.erase(str.size() - 1);

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 17, 2007
    #2
    1. Advertising

  3. Jack Klein Guest

    On Mon, 16 Apr 2007 20:40:32 -0400, "Victor Bazarov"
    <> wrote in comp.lang.c++:

    > wrote:
    > > I am looking for a way to take a floating point number and get rid of
    > > any extraneous 0's at the end. For instance if I have
    > >
    > > myFloat = 9999;
    > >
    > > printf("%f", myFloat);
    > >
    > > I get 9999.0000. I want to get rid of these 0's.
    > >
    > > Here is one catch. I also need to be able to do this:
    > >
    > > myFloat = .531;
    > >
    > > printf("%f", myFloat);
    > >
    > > I get .5310000
    > >
    > > I need to get .531.
    > >
    > > I realize I can do this
    > >
    > > printf("%2f", myFloat);
    > >
    > > which will limit the mantissa to two places, but this will not work
    > > for what i am doing because I can not lose that much precision on
    > > floats like .05369.

    >
    > The problem actually is rather simple. The default is 7 decimal


    Correction, 6 decimal places.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 17, 2007
    #3
  4. Guest

    On Apr 16, 6:40 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > I am looking for a way to take a floating point number and get rid of
    > > any extraneous 0's at the end. For instance if I have

    >
    > > myFloat = 9999;

    >
    > > printf("%f", myFloat);

    >
    > > I get 9999.0000. I want to get rid of these 0's.

    >
    > > Here is one catch. I also need to be able to do this:

    >
    > > myFloat = .531;

    >
    > > printf("%f", myFloat);

    >
    > > I get .5310000

    >
    > > I need to get .531.

    >
    > > I realize I can do this

    >
    > > printf("%2f", myFloat);

    >
    > > which will limit the mantissa to two places, but this will not work
    > > for what i am doing because I can not lose that much precision on
    > > floats like .05369.

    >
    > The problem actually is rather simple. The default is 7 decimal
    > digits in the result, which means that 0.5310000 and 0.530999951
    > will give you the same output. The zeros are there to indicate to
    > what digit the output was _rounded_ to.
    >
    > If you really need to lose the trailing zeros, you will have to
    > trim them yourself. Convert your number to a string and drop all
    > chars from it until the first non-zero:
    >
    > std::eek:stringstream s;
    > s << myFloat;
    > std::string str = s.str();
    > while (str.size() > 1 && *str.rbegin() == '0')
    > str.erase(str.size() - 1);
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    I figured as much. Thank you for taking the time to answer my
    question. You guys are great =)
    , Apr 17, 2007
    #4
  5. wrote:
    > I am looking for a way to take a floating point number and get rid of
    > any extraneous 0's at the end. For instance if I have
    >
    > myFloat = 9999;
    >
    > printf("%f", myFloat);
    >
    > I get 9999.0000. I want to get rid of these 0's.


    Have you tried "%g" instead of "%f"?
    Juha Nieminen, Apr 18, 2007
    #5
    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. Edith Gross
    Replies:
    2
    Views:
    337
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 2, 2003
  2. ben
    Replies:
    4
    Views:
    612
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,238
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    431
  5. guru
    Replies:
    8
    Views:
    277
Loading...

Share This Page