17

I've got a database filled up with doubles like the following one:

1.60000000000000000000000000000000000e+01

Does anybody know how to convert a number like that to a double in C++?

Is there a "standard" way to do this type of things? Or do I have to roll my own function?

Right now I'm doing sth like this:

#include <string>
#include <sstream>



int main() {
    std::string s("1.60000000000000000000000000000000000e+01");
    std::istringstream iss(s);
    double d;
    iss >> d;
    d += 10.303030;
    std::cout << d << std::endl;
}

Thanks!

miya
  • 1,059
  • 1
  • 11
  • 20
  • 2
    What type of database? What is the schema of the database around that field? – Brian R. Bondy Nov 10 '09 at 19:14
  • What is the precision you need to keep from this number? If, keep that high precision is not an issue, you can simple "cut" this string and convert it to double using ordinary C functions. – Andres Nov 10 '09 at 19:17
  • Note that you should check the string stream's state after reading from it. Reading can fail. – sbi Nov 10 '09 at 19:23
  • possible duplicate of [How do I convert a double into a string in C++?](http://stackoverflow.com/questions/332111/how-do-i-convert-a-double-into-a-string-in-c) – jb. May 22 '14 at 16:21
  • Not a duplicate, this is the opposite operation. – JasonMArcher May 22 '14 at 17:26

2 Answers2

21

Something like this? This would be the "C++" way of doing it...

#include <sstream>
using namespace std;

// ...

    string s = "1.60000000000000000000000000000000000e+01";
    istringstream os(s);
    double d;
    os >> d;
    cout << d << endl;

Prints 16.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 6
    If you've got boost, then `double d = boost::lexical_cast(s)` will do the same thing. – Mike Seymour Nov 10 '09 at 19:20
  • 1
    Is this supposed to work if we change the type to "int"? I seem to just get zero out, can the routine not handle the exponential notation, even though it is representing an integer? – Ben Farmer Mar 05 '16 at 22:25
  • [It's complicated](http://en.cppreference.com/w/cpp/locale/num_get/get) but probably not. – Thomas Mar 06 '16 at 17:57
  • it probably deserves another question but what about the other way around going from scientific to double? – mLstudent33 May 25 '20 at 05:50
  • As @BenFarmer says, it doesn't seem to work correctly to convert a string containing say "1e4" to "int". I am expecting 10000 but all I get is 1. Besides, I was expecting os.fail() to return true in this case so that atleast I can trap it (as suggested at https://www.oreilly.com/library/view/c-cookbook/0596007612/ch03s06.html), but that doesn't return true either, so this error escaped. Any suggestions how to reliably convert a E-notation (without decimal points) into an "int", and also trap errors reliably? – vharihar Dec 16 '21 at 07:25
  • @vharihar I suggest you open a new question for that. If one doesn't exist already. – Thomas Dec 16 '21 at 08:42
12

You want the standard c function atof ([A]SCII to [F]loat, but it actually uses doubles rather than floats).

Russell Newquist
  • 2,656
  • 2
  • 16
  • 18
  • 1
    This will be much faster than using C++ stream objects. – Heath Hunnicutt Nov 10 '09 at 19:18
  • 2
    @Heath: It's always easy to be fast when you skip important steps: `atof("0.0")` vs. `atof("blah")`. (Note: I'm not trying to defend C++ streams, they _are_ slower than they should be. But they do indicate errors in a unambiguous way.) – sbi Nov 10 '09 at 19:22
  • 4
    @sbi: `strtod` works just like `atof` except it allows you to detect errors unambiguously. – Ben Voigt Dec 18 '10 at 18:50
  • @Ben: I wouldn't have criticized an answer suggesting `strtod()` for being unable to report any errors. This answer, however, suggests `atof()`. – sbi Dec 18 '10 at 23:57
  • @sbi, right, the previous comment wasn't so much directed TO you as it was a follow-up and additional information – Ben Voigt Dec 19 '10 at 01:36