473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.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
Jul 22 '05 #1
5 2059
"Brandon" <bc******@creighton.edu> wrote in message
news:24**************************@posting.google.c 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.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[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
Jul 22 '05 #2
"Brandon" <bc******@creighton.edu> 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;
Jul 22 '05 #3

"Brandon" <bc******@creighton.edu> wrote in message news:24**************************@posting.google.c 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.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....uni-berlin.de.
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 22 '05 #4
"Alex Vinokur" <al****@big-foot.com> wrote in message news:<2m************@uni-berlin.de>...
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
Jul 22 '05 #5
> 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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
2
by: Colleyville Alan | last post by:
I am using Access and have embedded the ActiveX control Formula One that came with Office 2000. (ver 3.04). I have created and formatted a spreadsheet and now I want to copy the info with...
4
by: DBQueen | last post by:
I have a subform which is in Continuous Forms view. I have added a button to the bottom of the page to move to the next record using the button wizard (result: DoCmd.GoToRecord , , acNext). I...
4
by: Bradley | last post by:
I have an A2000 database in which I have a continuous form with a tick box. There is also a text box with a conditional format that is based on the expression , if it's true then change the...
1
by: GGerard | last post by:
Hello Is there a way to use a variable in the Conditional Formatting of a Textbox? Example : I want the background of a textbox in a continuous form to change color when the value of...
4
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
8
by: gopal | last post by:
Hi I am trying to write to log file but the formatting of string is not properly aligned in log file result The results looks some thing like this OK move.xml Successful OK ...
8
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated with the fields the conditional formatting works...
4
by: midlothian | last post by:
Hello, I have conditional formatting set up on a subform based on a calculated value in the underlying query. For instance, if Sales are >$1000, the query displays "Yes," otherwise it displays...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.