#include <iostream>
#include <iomanip>

int main()
{
    const auto coutwf = [] () -> std::ostream&
    { return std::cout << std::setw(20) << std::setfill('0') ; } ;

    constexpr double d = 12345678.9 ;
    coutwf() << d << '\n' ; // 000000001.23457e+007

    // save the current format flags associated with std::cout
    const auto original_flags = std::cout.flags() ;

    // set flag: use fixed notation for floating point types
    // set flag: prefix non-negative numeric output with a '+'
    // leave all other format flags unchanged
    std::cout.setf( std::ios::fixed | std::ios::showpos ) ;
    coutwf() << d << '\n' ; // 0000+12345678.900000

    // set: use internal justification
    // unset: left justification, right justification
    // leave all other format flags unchanged
    std::cout.setf( std::ios::internal, std::ios::adjustfield ) ;
    coutwf() << d << '\n' ; // +000012345678.900000

    // set: use left justification
    // unset: right justification, internal justification
    // leave all other format flags unchanged
    std::cout.setf( std::ios::left, std::ios::adjustfield ) ;
    coutwf() << d << '\n' ; // +12345678.9000000000

    // replace *all* flags with the original flags
    std::cout.flags(original_flags) ;
    coutwf() << d << '\n' ; // 000000001.23457e+007
}
