Converting sprintf to ostringstream

J

jasonwthompson

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);
 
V

Victor Bazarov

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
 
J

jasonwthompson

Ah... I see. I was trying too hard. Yes it does do it like the %g
by default. Thanks!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top