user1291579
user1291579

Reputation: 11

format matrix with integers and double precision in C++

How could you print integers and double precision values in a matrix. ex.(100 34 56 77.80 75 45 98 22.00 I am able to print the matrix except that the double values with trailing zeroes do not display the trailing zero. I believe the answer lies within the library but I have tried multiple combinations with no luck. Need help.

Upvotes: 1

Views: 1933

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477110

<iomanip> is indeed the way to go:

#include <iomanip>
#include <iostream>

for (unsigned int i = 0; i != nrows; ++i)
{
    for (unsigned int j = 0; j != ncols; ++j)
    {
        if (j != 0) std::cout << " ";
        std::cout << std::setw(5) << std::setfill(' ') << std::setprecision(2)
                  << static_cast<double>(data[i][j]);
    }
    std::cout << "\n";
}

Upvotes: 3

Related Questions