char buffer[20];
double d = 1.23456 ;
sprintf(buffer, "%f", d);
MessageBox(NULL, buffer, "Without precision", MB_OK);
sprintf(buffer, "%.2f", d);
MessageBox(NULL, buffer, "With precision", MB_OK);
Main Topics
Browse All TopicsUsing the "setprecision(n)" manipulator, you can increase/decrease decimal places, but it can only be used in standard output (cout), well I least I think it does ...
So how do you use it in a Windows enviroment, in Visual C++, without the "<<"?
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
First off, if you have an integer it has no decimal places, if you have a floating point or double type you can format a value with precision.
Use the stringstream just like any other stream, a stringstream is a stream it just output the result in a memory buffer (a string using the ss.str() method to retrieve it) instead of outputting the result to a file or console.
So, if you know how to output a double type with precision to a console you can also make it to a string by using a stringstream instead of cout.
Exactly how you output that string to a window depends on what kind of platform you're on. In Visual C++ using MFC the way you would output the data would then be to first output to a ostringstream then retrieve the output as a string using ss.str() and just output that string to the window.
Something like this:
#include <sstream>
#include <iomanip>
void do_something()
{
int k = 15;
double x = 3.14;
ostringstream ss;
ss << "k = " << k << ", x = " << setprecision(3) << x;
// Now, the ss.str is the string "k = 15, x = 3.14"
}
So your question boils down to: "How do I set a string to a label". I believe in Visual C++ in MFC you can set the label by:
m_label.SetWindowText(ss.s
Note that this will convert the ss.str() to a CString string.
You can also use the Win32 function: ::SetWindowText( m_label -> m_hwnd, ss.str()); This will NOT convert the string to a CString and will not involve a call to heap allocation. This latter call is therefore to be preferred if you want as little overhead as possible.
Alf
The ss above is the name of the stringstream I use. Since it is a local variable there shouldn't be any need to make a fuzz about the variable name. If you like you can call it whatever you like:
ostringstream blah;
blah << setprecision(n) << dblnum;
now blah.str() will be a C-style string holding the value of dblnum with the given precision.
Alf
Business Accounts
Answer for Membership
by: EarthQuakerPosted on 2003-07-06 at 10:21:42ID: 8864309
"setprecision()" only affects cout ( or maybe all that inherits from ostream ).
So its only use is for printing to a console application.
If you are doing win32 application, you display things in textboxes or others things, so if you want
something else than the default precision you have to set it yourself.
Example is coming.