Formatting in C++

Discussion in 'C++' started by Brandon, Jul 20, 2004.

  1. Brandon

    Brandon Guest

    In a program, I'm using an ostringstream to convert a floating point
    value to a string. When the numbers are big enough, they are
    converted using scientific notation. To fix this, I changed the code
    to
    oStream << fixed << myFloat;
    but now there are an unnecessary amount of trailing zeros that I don't
    want. I seem to recall something in C, using printf(), where you
    could specify how many places you wanted. For examples, using
    something like printf("%1.2", 0.45678) would print the text "0.45".
    Is there any way to force a certain number of decimal places in C++
    using ios streams (ostringstreams in particular)?

    -Brandon
     
    Brandon, Jul 20, 2004
    #1
    1. Advertising

  2. Brandon

    David Hilsee Guest

    "Brandon" <> wrote in message
    news:...
    > In a program, I'm using an ostringstream to convert a floating point
    > value to a string. When the numbers are big enough, they are
    > converted using scientific notation. To fix this, I changed the code
    > to
    > oStream << fixed << myFloat;
    > but now there are an unnecessary amount of trailing zeros that I don't
    > want. I seem to recall something in C, using printf(), where you
    > could specify how many places you wanted. For examples, using
    > something like printf("%1.2", 0.45678) would print the text "0.45".
    > Is there any way to force a certain number of decimal places in C++
    > using ios streams (ostringstreams in particular)?


    I think you might be searching for std::setprecision() (<iomanip>) or
    std::ios_base::precision().

    int main() {
    double testData[] = {0.00000001, 1.0, 20.0, 1000000000000000.0,
    std::numeric_limits<double>::max()};
    int numElems = sizeof(testData)/sizeof(testData[0]);
    for ( int i = 0; i < numElems; ++i ) {
    std::cout << testData << std::endl;
    }

    std::cout << std::fixed << std::setprecision(2);
    std::cout << "After modifying stream:" << std::endl;
    for ( int i = 0; i < numElems; ++i ) {
    std::cout << testData << std::endl;
    }

    return 0;
    }

    Output (Win2K, VC++. Net 2003):
    1e-008
    1
    20
    1e+015
    1.79769e+308
    After modifying stream:
    0.00
    1.00
    20.00
    1000000000000000.00
    1797693134862318000000000000000000000000000000000000000000000000000000000000
    0000
    0000000000000000000000000000000000000000000000000000000000000000000000000000
    0000
    0000000000000000000000000000000000000000000000000000000000000000000000000000
    0000
    000000000000000000000000000000000000000000000000000000000000000000000.00

    --
    David Hilsee
     
    David Hilsee, Jul 20, 2004
    #2
    1. Advertising

  3. Brandon

    Siemel Naran Guest

    "Brandon" <> wrote in message

    > Is there any way to force a certain number of decimal places in C++
    > using ios streams (ostringstreams in particular)?


    You can use setprecision.

    ostringstream o;
    o << setprecision(2);
    o << 1.2345;
    o << 6.7890;
     
    Siemel Naran, Jul 20, 2004
    #3
  4. Brandon

    Alex Vinokur Guest

    "Brandon" <> wrote in message news:...
    > In a program, I'm using an ostringstream to convert a floating point
    > value to a string. When the numbers are big enough, they are
    > converted using scientific notation. To fix this, I changed the code
    > to
    > oStream << fixed << myFloat;
    > but now there are an unnecessary amount of trailing zeros that I don't
    > want. I seem to recall something in C, using printf(), where you
    > could specify how many places you wanted. For examples, using
    > something like printf("%1.2", 0.45678) would print the text "0.45".
    > Is there any way to force a certain number of decimal places in C++
    > using ios streams (ostringstreams in particular)?
    >
    > -Brandon


    You might use also C-like printf format with C++ ostream; float format sample can be seen at
    http://groups.google.com/groups?selm=bog0bi$1d8s5g$-berlin.de.


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jul 20, 2004
    #4
  5. Brian McGuinness, Jul 20, 2004
    #5
  6. Brandon

    Brandon Guest

    > I think you might be searching for std::setprecision() (<iomanip>) or
    > std::ios_base::precision().


    Yeah, that was it. I haven't had much use for stream formatting
    lately and forgot about it. Thanks for the help.

    -Brandon
     
    Brandon, Jul 23, 2004
    #6
    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. thaneesh

    Code formatting rules in editor

    thaneesh, Jul 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,161
    thaneesh
    Jul 2, 2003
  2. Lee Simpson

    Re: Formatting string in WHERE clause

    Lee Simpson, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,261
    Lee Simpson
    Jul 18, 2003
  3. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,019
    Oliver Sturm
    Oct 13, 2005
  4. ugly
    Replies:
    0
    Views:
    506
  5. Sonali.NET[MVP]
    Replies:
    2
    Views:
    24,083
    TimDay
    Jul 30, 2006
Loading...

Share This Page