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
    #1
    1. Advertising

  2. 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 8, 2006
    #2
    1. Advertising

  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
    #3
  4. 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
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eoin Mcloughlin

    Decimal places and huge fractions.

    Eoin Mcloughlin, Feb 11, 2004, in forum: Perl
    Replies:
    5
    Views:
    1,595
    Eoin Mcloughlin
    Feb 13, 2004
  2. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    327
    Curt_C [MVP]
    May 18, 2004
  3. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    534
    Curt_C [MVP]
    May 18, 2004
  4. Harry

    Remove Decimal Places

    Harry, Jun 3, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    8,913
    VB Programmer
    Jun 3, 2004
  5. Bharat Bhushan

    Decimal Places

    Bharat Bhushan, Aug 8, 2003, in forum: Java
    Replies:
    2
    Views:
    477
    Lee Weiner
    Aug 8, 2003
Loading...

Share This Page