overloading
overloading

Reputation: 1200

Convert string represented in scientific notation to float and double

So I am working on this project where I have to read in a CSV file which contains numbers that are represented in scientific notation. So far what I have is to read in each number then convert that number into string then convert it to double with stringstream but I am not sure if this is the correct way to do it. Any advice / suggestion would be helpful!

double temp;
istringstream in(line); //line is the string which contains the number, ex: 3.30144800e+03
in >> temp;
arr[w++] = temp;

Upvotes: 1

Views: 4711

Answers (1)

Captain Obvlious
Captain Obvlious

Reputation: 20093

You can convert a string to a double with std::atof

istringstream in(line);
double        value(atof(in.str().c_str()));

OR you can simply stream the double out of the buffer...

double value;
in >> value;

Upvotes: 1

Related Questions