fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sstream>
  4.  
  5. class MyFloat
  6. {
  7. public:
  8. MyFloat(float val = 0) : _val(val)
  9. {}
  10.  
  11. friend std::ostream& operator<<(std::ostream& os, const MyFloat& rhs)
  12. { os << MyFloat::noLeadingZero(rhs._val, os); }
  13.  
  14. private:
  15. static std::string noLeadingZero(float val, std::ostream& os)
  16. {
  17. std::stringstream ss;
  18. ss.copyfmt(os);
  19. ss << val;
  20. std::string str = ss.str();
  21.  
  22. if(val > 0.f && val < 1.f)
  23. return str.substr(1, str.size()-1);
  24. else if(val < 0.f && val > -1.f)
  25. return "-" + str.substr(2, str.size()-1);
  26.  
  27. return str;
  28. }
  29. float _val;
  30. };
  31.  
  32. int main()
  33. {
  34. std::cout << "Value: " << std::setw(2) << std::setprecision(1);
  35. std::cout << std::fixed << MyFloat(0.357765);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Value: .4