Write 3 decimal places always to stream

M

moleskyca1

How can I write 3 decimal places always to stream?

For example, I wrote:


const double d1 = 1.77777777;
const double d2 = 12.7777777;
const double d3 = 123.777777;

cout << "d1 = " << setw(3) << d1 << endl;
cout << "d2 = " << setw(3) << d2 << endl;
cout << "d3 = " << setw(3) << d3 << endl;

I get this
d1 = 1.77
d2 = 12.7
d3 = 123,

but I want this:

d1 = 1.778
d2 = 12.778
d3 = 123.778

How can I do it with stream (using printf, you would use %.3f)?

Thank you.
 
V

Victor Bazarov

How can I write 3 decimal places always to stream?

For example, I wrote:


const double d1 = 1.77777777;
const double d2 = 12.7777777;
const double d3 = 123.777777;

cout << "d1 = " << setw(3) << d1 << endl;
cout << "d2 = " << setw(3) << d2 << endl;
cout << "d3 = " << setw(3) << d3 << endl;

I get this
d1 = 1.77
d2 = 12.7
d3 = 123,

but I want this:

d1 = 1.778
d2 = 12.778
d3 = 123.778

How can I do it with stream (using printf, you would use %.3f)?

See 'setprecision'. What book are you reading that does describe
'setw' but doesn't describe 'setprecision'?

V
 
M

moleskyca1

Victor said:
(e-mail address removed) wrote:
See 'setprecision'. What book are you reading that does describe
'setw' but doesn't describe 'setprecision'?

thank you, it works. I was reading the VC++ help files and they have
setw() and setfill() but not precision.
 
V

Victor Bazarov

thank you, it works. I was reading the VC++ help files and they have
setw() and setfill() but not precision.

Reminds me of an old joke...
"- You know that Pavarotti?.. Nothing special, I tell you."
"- Well... Did you hear him in a movie or a tape recording?"
"- Nah... My neighbor Johnny sang me a few parts."

Get yourself a decent book, and only resort to reading VC++ help files
when you need help on VC++ itself, like IDE, compiler options, etc.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top