Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Formatting in C++

 
Thread Tools Search this Thread
Old 07-20-2004, 04:09 AM   #1
Default Formatting in C++


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.4567 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
  Reply With Quote
Old 07-20-2004, 05:14 AM   #2
David Hilsee
 
Posts: n/a
Default Re: Formatting in C++

"Brandon" <> wrote in message
news: om...
> 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.4567 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:recision().

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[i] << 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[i] << 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
17976931348623180000000000000000000000000000000000 00000000000000000000000000
0000
00000000000000000000000000000000000000000000000000 00000000000000000000000000
0000
00000000000000000000000000000000000000000000000000 00000000000000000000000000
0000
00000000000000000000000000000000000000000000000000 0000000000000000000.00

--
David Hilsee


  Reply With Quote
Old 07-20-2004, 05:20 AM   #3
Siemel Naran
 
Posts: n/a
Default Re: Formatting in C++

"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;


  Reply With Quote
Old 07-20-2004, 06:24 AM   #4
Alex Vinokur
 
Posts: n/a
Default Re: Formatting in C++


"Brandon" <> wrote in message news: om...
> 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.4567 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....uni-berlin.de.


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




  Reply With Quote
Old 07-20-2004, 07:17 PM   #5
Brian McGuinness
 
Posts: n/a
Default Re: Formatting in C++

"Alex Vinokur" <> wrote in message news:<>...
> You might use also C-like printf format with C++ ostream; float format sample can be seen at
> http://groups.google.com/groups?selm....uni-berlin.de.


Boost already has this:

http://www.boost.org/libs/format/index.html

Much more convenient than the standard cout manipulators.

--- Brian
  Reply With Quote
Old 07-23-2004, 11:04 PM   #6
Brandon
 
Posts: n/a
Default Re: Formatting in C++

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


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

-Brandon
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump