fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. int main()
  5. {
  6. const auto coutwf = [] () -> std::ostream&
  7. { return std::cout << std::setw(20) << std::setfill('0') ; } ;
  8.  
  9. constexpr double d = 12345678.9 ;
  10. coutwf() << d << '\n' ; // 000000001.23457e+007
  11.  
  12. // save the current format flags associated with std::cout
  13. const auto original_flags = std::cout.flags() ;
  14.  
  15. // set flag: use fixed notation for floating point types
  16. // set flag: prefix non-negative numeric output with a '+'
  17. // leave all other format flags unchanged
  18. std::cout.setf( std::ios::fixed | std::ios::showpos ) ;
  19. coutwf() << d << '\n' ; // 0000+12345678.900000
  20.  
  21. // set: use internal justification
  22. // unset: left justification, right justification
  23. // leave all other format flags unchanged
  24. std::cout.setf( std::ios::internal, std::ios::adjustfield ) ;
  25. coutwf() << d << '\n' ; // +000012345678.900000
  26.  
  27. // set: use left justification
  28. // unset: right justification, internal justification
  29. // leave all other format flags unchanged
  30. std::cout.setf( std::ios::left, std::ios::adjustfield ) ;
  31. coutwf() << d << '\n' ; // +12345678.9000000000
  32.  
  33. // replace *all* flags with the original flags
  34. std::cout.flags(original_flags) ;
  35. coutwf() << d << '\n' ; // 000000001.23457e+007
  36. }
  37.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
0000000001.23457e+07
0000+12345678.900000
+000012345678.900000
+12345678.9000000000
0000000001.23457e+07