fork download
  1. #include <iostream>
  2. #include <locale>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. // a num_put facet to add a padding space for positive numbers
  7. class sign_padding :public std::num_put<char> {
  8. public:
  9. // only for float and double
  10. iter_type do_put(iter_type s, std::ios_base& f,
  11. char_type fill, double v) const
  12. {
  13. if (std::signbit(v) == false)
  14. *s++ = ' ';
  15. return std::num_put<char>::do_put(s, f, fill, v);
  16. }
  17. };
  18.  
  19. int main()
  20. {
  21. // add the facet to std::cout
  22. std::cout.imbue(std::locale(std::cout.getloc(), new sign_padding));
  23.  
  24. // now print what you want to print
  25. std::cout << -56.0f << "\n"
  26. << +56.0f << "\n"
  27. << -0.0f << "\n"
  28. << +0.0f << "\n";
  29.  
  30. // output:
  31. // -56
  32. // 56
  33. // -0
  34. // 0
  35. }
  36.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
-56
 56
-0
 0