5

I'm trying to convert a string into a double but my double gets cut off at the 3rd decimal point.

My string looks like this: "-122.39381636393" After it gets converted it looks like this: -122.394

void setLongitude(string longitude){
    this->longitude = (double)atof(longitude.c_str());

    cout << "got longitude: " << longitude << endl;
    cout << "setting longitude: " << this->longitude << endl;
}

Output example:

got longitude: -122.39381636393
setting longitude: -122.394

I want it to maintain all the decimal points, any tips?

jshah
  • 1,599
  • 2
  • 17
  • 38

3 Answers3

6

I would write this code if I were you:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "-122.39381636393";
    std::cout.precision(20);
    cout << "setting longitude: " << stod(str) << endl;
    return 0;
}

Basically, you would change things like:

  • precision for the printing

  • stod rather than low-level operation to get the double back from the string.

You can see it on ideone running.

László Papp
  • 51,870
  • 39
  • 111
  • 135
3

It is probably the printing that is truncating precision, not the conversion from string to double.

Look at ios_base::precision http://www.cplusplus.com/reference/ios/ios_base/precision/

e.g. cout.precision(10); cout << "setting longitude: " << this->longitude << endl;

teambob
  • 1,994
  • 14
  • 19
  • Also you might want to look at boost::lexical_cast for the string to double conversion. http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html – teambob May 03 '14 at 21:30
  • This will not work. The OP needs at least precision 11. – László Papp May 03 '14 at 21:33
  • Thanks! This works with precision 16. – jshah May 03 '14 at 21:34
  • @jshah: nope, 14, the sign and dot do not count, but your original code is really hack'ish not using existing functionality (strtod/stod). Also, the double cast is superfluous since that is the return value of atof by definition if you look [the man page up](http://en.cppreference.com/w/cpp/string/byte/atof). – László Papp May 03 '14 at 21:46
  • If you are using C++11 stod() may be a good idea, instead of lexical_cast() – teambob May 03 '14 at 21:49
2

The proper C++11 solution is to use stod - String TO Double. You probably should use a try ... catch around that function as it throws an exception if your string is not a valid number.

However, the code you have, using atof is perfectly [assuming no bugs in your particular standard C library] converting to double (despite the name being Ascii TO Float, it returns a double value), you are just not printing enough digits, use precision or setprecision to inform cout how many digits to use, e.g.

cout << "Setting longitude: " << setprecision(15) << this->longitude << endl;

You will need to include <iomanip> for setprecision to work.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227