# Write all numbers with 3 decimals?

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

1. ### desktopGuest

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

2. ### Victor BazarovGuest

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

Victor Bazarov, Sep 11, 2007

3. ### Scott GiffordGuest

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
4. ### James KanzeGuest

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.fill( '0' ) ;
// ...
// for each element...
std: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