Formatting output in C++

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

Leave a Comment