Velocity Reviews > C++ > Write all numbers with 3 decimals?

# Write all numbers with 3 decimals?

desktop
Guest
Posts: n/a

 09-11-2007
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[i][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

Victor Bazarov
Guest
Posts: n/a

 09-11-2007
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[i][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
--

Scott Gifford
Guest
Posts: n/a

 09-11-2007
desktop <(E-Mail Removed)> 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[i][j] << " ";

For more details, see:

http://www.fredosaurus.com/notes-cpp...ipulators.html

-----Scott.

James Kanze
Guest
Posts: n/a

 09-12-2007
On Sep 11, 11:52 pm, desktop <(E-Mail Removed)> 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[i][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[i][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:(E-Mail Removed)
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