# Write 3 decimal places always to stream

Discussion in 'C++' started by moleskyca1@yahoo.com, Aug 8, 2006.

1. ### Guest

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.

, Aug 8, 2006

2. ### Victor BazarovGuest

wrote:
> 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
--

Victor Bazarov, Aug 8, 2006

3. ### Guest

Victor Bazarov wrote:
> 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.

, Aug 8, 2006
4. ### Victor BazarovGuest

wrote:
> Victor Bazarov wrote:
>> 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.

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.

Victor Bazarov, Aug 8, 2006