fork(4) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. std::string getFloatWithoutLeadingZero(float val)
  7. {
  8. std::stringstream ss;
  9. ss << std::setw(2) << std::setprecision(1);
  10. ss << std::fixed << val;
  11.  
  12. std::string str = ss.str();
  13. if(val > 0.f && val < 1.f)
  14. return str.substr(1, str.size()-1);
  15. else if(val < 0.f && val > -1.f)
  16. return "-" + str.substr(2, str.size()-1);
  17. return str;
  18. }
  19.  
  20. int main()
  21. {
  22. std::cout << getFloatWithoutLeadingZero( .1337f) << std::endl;
  23. std::cout << getFloatWithoutLeadingZero(-.1337f) << std::endl;
  24. std::cout << getFloatWithoutLeadingZero( 42.f) << std::endl;
  25. std::cout << getFloatWithoutLeadingZero(-42.f) << std::endl;
  26.  
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
.1
-.1
42.0
-42.0