Write all numbers with 3 decimals?

Discussion in 'C++' started by desktop, Sep 11, 2007.

  1. desktop

    desktop Guest

    I have a program that writes something like this:

    0 0 -1 0
    -0.6 0.8 0 0
    0.8 0 0 0
    0 0 0 1


    I write that with this statement:

    std::cout << m[j] << " ";

    in a loop. Is there some way to print all numbers with 3 decimals
    including the potential minus, so I get something like this:

    0.00 -1.0
    -0.6 0.80
    0.80 0.00

    etc
    desktop, Sep 11, 2007
    #1
    1. Advertising

  2. desktop wrote:
    > I have a program that writes something like this:
    >
    > 0 0 -1 0
    > -0.6 0.8 0 0
    > 0.8 0 0 0
    > 0 0 0 1
    >
    >
    > I write that with this statement:
    >
    > std::cout << m[j] << " ";
    >
    > in a loop. Is there some way to print all numbers with 3 decimals
    > including the potential minus, so I get something like this:
    >
    > 0.00 -1.0
    > -0.6 0.80
    > 0.80 0.00


    See 'std::fixed', 'std::setw', 'std::setprecision' and other I/O
    stream manipulators. The header is <iomanip>.

    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, Sep 11, 2007
    #2
    1. Advertising

  3. desktop <> writes:


    [...]

    > in a loop. Is there some way to print all numbers with 3 decimals
    > including the potential minus, so I get something like this:


    Something like this should work:

    std::cout << fixed << setprecision(3) << m[j] << " ";

    For more details, see:

    http://www.fredosaurus.com/notes-cpp/io/omanipulators.html

    -----Scott.
    Scott Gifford, Sep 12, 2007
    #3
  4. desktop

    James Kanze Guest

    On Sep 11, 11:52 pm, desktop <> wrote:
    > I have a program that writes something like this:


    > 0 0 -1 0
    > -0.6 0.8 0 0
    > 0.8 0 0 0
    > 0 0 0 1


    > I write that with this statement:


    > std::cout << m[j] << " ";


    > in a loop. Is there some way to print all numbers with 3 decimals
    > including the potential minus, so I get something like this:


    > 0.00 -1.0
    > -0.6 0.80
    > 0.80 0.00


    Not directly. First of all, there is absolutely no way to get
    the minus sign to be counted as a digit. And even if all of the
    values were positive, I don't think you could do it. By
    formatting each value into a stringstream, then formatting the
    resulting string, it can be done, however:

    // before outputting the table...
    std::cout.setf( std::ios::left, std::ios::adjustfield ) ;
    std::cout.fill( '0' ) ;
    // ...
    // for each element...
    std::eek:stringstream tmp ;
    tmp.setf( std::ios::showpoint ) ;
    tmp.precision( 1 ) ;
    tmp << table[j] ;
    std::cout << ' ' << std::setw( 4 ) << tmp.str() ;

    In practice, of course, you'll definitly want to save the
    stream's formatting state, and restore it afterwards; I doubt
    that your collegues would appreciate the fact that the fill
    character has suddenly become '0' after your function has been
    called.

    And are you sure that this is what you want. It's a very
    unusual format, and typically, something which aligns the
    decimals would be more appropriate. If you're doing any numeric
    work at all, you doubtlessly already have a custom manipulator
    for doing this, so you could just write:
    std::cout << ' ' << FFmt( 5, 2 ) << table[ i ][ j ] ;
    or something like that. Correctly written, FFmt also eliminates
    the need for saving and restoring the format flags, so your
    format modifications don't affect others.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 12, 2007
    #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. Kerri

    Formatting Decimals

    Kerri, Aug 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Steve C. Orr, MCSD
    Aug 21, 2003
  2. Kevin Spencer

    Re: Formatting Decimals

    Kevin Spencer, Aug 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    396
    Kevin Spencer
    Aug 26, 2003
  3. Brent Burkart

    asp.net text box for decimals

    Brent Burkart, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    497
    Chris Jackson
    Nov 12, 2003
  4. PerlFAQ Server
    Replies:
    0
    Views:
    116
    PerlFAQ Server
    Jan 16, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    128
    PerlFAQ Server
    Apr 17, 2011
Loading...

Share This Page