Formatting output in C++

前端 未结 6 1183
既然无缘
既然无缘 2020-12-01 09:52

In a C++ code I have a matrix of double variables which I print out. However because all of them have different number of digits, the output format is destroyed. One solutio

6条回答
  •  半阙折子戏
    2020-12-01 09:56

    Take a look at stream manipulators, especially std::setw and std::setfill.

    float f = 3.1415926535;
    std::cout << std::setprecision(5)   // precision of floating point output
              << std::setfill(' ')      // character used to fill the column
              << std::setw(20)          // width of column
              << f << '\n';             // your number
    

提交回复
热议问题