String in scientific notation C++ to double conversion
Asked Answered
M

2

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!

Mccord answered 10/11, 2009 at 19:12 Comment(5)
What type of database? What is the schema of the database around that field?Unesco
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.Fortyniner
Note that you should check the string stream's state after reading from it. Reading can fail.Nation
possible duplicate of How do I convert a double into a string in C++?Cerenkov
Not a duplicate, this is the opposite operation.Canice
R
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.

Reamonn answered 10/11, 2009 at 19:15 Comment(6)
If you've got boost, then double d = boost::lexical_cast<double>(s) will do the same thing.Lynn
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?Overcome
It's complicated but probably not.Reamonn
it probably deserves another question but what about the other way around going from scientific to double?Kenrick
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 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?Glaswegian
@Glaswegian I suggest you open a new question for that. If one doesn't exist already.Reamonn
U
12

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

Undecagon answered 10/11, 2009 at 19:14 Comment(5)
This will be much faster than using C++ stream objects.Mateo
@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.)Nation
@sbi: strtod works just like atof except it allows you to detect errors unambiguously.Deconsecrate
@Ben: I wouldn't have criticized an answer suggesting strtod() for being unable to report any errors. This answer, however, suggests atof().Nation
@sbi, right, the previous comment wasn't so much directed TO you as it was a follow-up and additional informationDeconsecrate

© 2022 - 2024 — McMap. All rights reserved.