Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Converting sprintf to ostringstream

Reply
Thread Tools

Converting sprintf to ostringstream

 
 
jasonwthompson@gmail.com
Guest
Posts: n/a
 
      08-23-2007
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);

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-23-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
jasonwthompson@gmail.com
Guest
Posts: n/a
 
      08-23-2007
Ah... I see. I was trying too hard. Yes it does do it like the %g
by default. Thanks!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: using ZLIB to compress std::ostringstream? Mike C++ 1 08-03-2003 06:50 PM
Re: ? How to clear ostringstream buffer Mark Warren C++ 3 07-23-2003 10:12 AM
vector of ostringstream Alex Vinokur C++ 4 07-14-2003 02:36 AM
Re: std::ostringstream unexpected behavior with .net 2003. Russell Hanneken C++ 0 06-25-2003 10:22 PM
Re: std::ostringstream unexpected behavior with .net 2003. Victor Bazarov C++ 0 06-25-2003 10:20 PM



Advertisments