Formatting output in C++
Formatting output in C++ 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 solution is to do cout.precision(5) but I want different columns have a different precision. Also, because there are negative values in some cases, the presence of the - sign also causes problems. How to get around this and produce a properly formatted output? cout.precision(5) - 6 Answers 6 Off the top of my head, you can use setw(int) to specify the width of the output. like this: std::cout << std::setw(5) << 0.2 << std::setw(10) << 123456 << std::endl; std::cout << std::setw(5) << 0.12 << std::setw(10) << 123456789 << std::endl; gives this: 0.2 123456 0.12 123456789 just FYI : while doing std::setw(x) make sure...