Converting sprintf to ostringstream

Discussion in 'C++' started by jasonwthompson@gmail.com, Aug 23, 2007.

  1. Guest

    I'm converting some old C code to C++. Although I'm not concerned
    about converting every C item to C++ I'm in a situation in which using
    an ostringstream makes more sense than using sprintf. Here's my
    problem: In sprintf, one could use the %g symbol to indicate that I
    either wanted scientific notation or a simple floating point,
    whichever is smaller. Is there a way to do that using an
    ostringstream?

    Basically, what is the ostringstream equivalent of:

    sprintf(buffer, "%-20.12g",mynum);
    , Aug 23, 2007
    #1
    1. Advertising

  2. wrote:
    > I'm converting some old C code to C++. Although I'm not concerned
    > about converting every C item to C++ I'm in a situation in which using
    > an ostringstream makes more sense than using sprintf. Here's my
    > problem: In sprintf, one could use the %g symbol to indicate that I
    > either wanted scientific notation or a simple floating point,
    > whichever is smaller. Is there a way to do that using an
    > ostringstream?
    >
    > Basically, what is the ostringstream equivalent of:
    >
    > sprintf(buffer, "%-20.12g",mynum);


    I would think it does that by default, no? Just output 'mynum' and
    it should do what you need. As to '20' and '12', look for the 'setw'
    and 'setprecision' manipulators.

    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, Aug 23, 2007
    #2
    1. Advertising

  3. Guest

    Ah... I see. I was trying too hard. Yes it does do it like the %g
    by default. Thanks!
    , Aug 23, 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. Victor Bazarov
    Replies:
    0
    Views:
    844
    Victor Bazarov
    Jun 25, 2003
  2. Russell Hanneken
    Replies:
    0
    Views:
    888
    Russell Hanneken
    Jun 25, 2003
  3. Alex Vinokur

    vector of ostringstream

    Alex Vinokur, Jul 10, 2003, in forum: C++
    Replies:
    4
    Views:
    686
    llewelly
    Jul 14, 2003
  4. Mark Warren
    Replies:
    3
    Views:
    16,539
    Sam Holden
    Jul 23, 2003
  5. Mike
    Replies:
    1
    Views:
    979
    John Harrison
    Aug 3, 2003
Loading...

Share This Page