如何在矩阵中打印整数和 double 值。 例如(100 34 56 77.80 75 45 98 22.00 我能够打印矩阵,除了带有尾随零的 double 值不显示尾随零。 我相信答案就在图书馆里,但我尝试了多种组合但没有成功。 需要帮助。
最佳答案
<iomanip>
确实是要走的路:
#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";
}
关于c++ - 在 C++ 中使用整数和 double 格式化矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9862444/