fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. double string_to_double( const std::string& s )
  7. {
  8. std::istringstream i(s);
  9. double x;
  10. if (!(i >> x))
  11. return 0;
  12. return x;
  13. }
  14.  
  15. double parseFloat(const std::string& input){
  16. const char *p = input.c_str();
  17. if (!*p || *p == '?')
  18. return -1;
  19. int s = 1;
  20. while (*p == ' ') p++;
  21.  
  22. if (*p == '-') {
  23. s = -1; p++;
  24. }
  25.  
  26. double acc = 0;
  27. while (*p >= '0' && *p <= '9')
  28. acc = acc * 10 + *p++ - '0';
  29.  
  30. if (*p == '.') {
  31. double k = 0.1;
  32. p++;
  33. while (*p >= '0' && *p <= '9') {
  34. acc += (*p++ - '0') * k;
  35. k *= 0.1;
  36. }
  37. }
  38. if (*p) cout << ("Invalid numeric format");
  39. return s * acc;
  40. }
  41.  
  42. int main() {
  43. double val = parseFloat("132.12345645645645");
  44. cout.precision(20);
  45. cout << val;
  46. return 0;
  47. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
132.12345645645643799