0

I converted a string to a double using ::atof, it converts OK but it rounds up the decimal and I don't want it to.

string n;
double p;

cout << "String? :" << endl;
cin >> n

p = ::atof(n.c_str());
cout << p << endl;

I usually type in numbers like 123,456.78, 12,345.87, 123,456,789.12. When I type in a smaller number like 1,234.83 or bigger the programs starts messing with the decimals.

It would be of huge help if anybody helps. Thanks!

  • 2
    Why not `cin >> p`? – tadman Jan 17 '19 at 23:48
  • 4
    [Extend the displayed precision](https://en.cppreference.com/w/cpp/io/manip/setprecision). – user4581301 Jan 17 '19 at 23:50
  • @NeilButterworth why do you say so? Is there a easier way to convert a string into a double? – javier santisteban Jan 17 '19 at 23:51
  • [strtod](https://en.cppreference.com/w/cpp/string/byte/strtof) and [std::stod](https://en.cppreference.com/w/cpp/string/basic_string/stof). Both allow you to catch a number of failure cases that `atof` merely "reports" by returning 0.0. – user4581301 Jan 17 '19 at 23:53
  • Near miss duplicate: [Printing the correct number of decimal points with cout](https://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout) – user4581301 Jan 17 '19 at 23:56
  • 2
    Are you aware of this: https://wandbox.org/permlink/nQLEN529EJm1Ku2S ? – Bob__ Jan 18 '19 at 00:02
  • 1
    Both C and C++ provide a number of ways of converting a string to a double - atof is the only one that does not provide a means of checking if the conversion actually worked. –  Jan 18 '19 at 00:04
  • @user4581301 Okay, and how would I use those in this case? – javier santisteban Jan 18 '19 at 00:13
  • @NeilButterworth Oh okay, and what do you recommend that I should be using to check if the conversion worked? – javier santisteban Jan 18 '19 at 00:15
  • @javiersantisteban My answer shows you how to use `stod` and `setprecision`. You should be able to cobble together working code from all the comments and answer(s) now. – Chimera Jan 18 '19 at 00:20
  • `p = std::stod(n);` is about as easy as it gets, assuming your compiler supports C++11 (at this point it sure as should) and C++11 support is enabled (g++ option `-std=c++11`). If `stod` can't convert, an exception is thrown. You can check if there is crap left over in the string by providing the optional `pos` parameter and making sure that it's at the end of the string. – user4581301 Jan 18 '19 at 00:20
  • If you have to use an older C++ standard or bad inputs are't going to be exceptional enough to make using exceptions make sense, `strtod` has a good example at the bottom of [this documentation page](https://en.cppreference.com/w/cpp/string/byte/strtof). It's too messy to go through in a comment. – user4581301 Jan 18 '19 at 00:23

1 Answers1

1

You need to set the precision used when sending data to the output stream using setprecision as shown below.

Of course the problem with this code is that atof() isn't your best option. However, to answer your question the use of atof() doesn't matter.

#include <iomanip>  
#include <iostream>  
#include <string>

int main()
{
    double x;
    std::string n;

    std::cout << "String? :" << std::endl;
    std::cin >> n;

    x = ::atof(n.c_str());
    std::cout << std::setprecision(10) << x << std::endl;
}

To convert and catch conversion errors you could use the following:

try
{
    x = std::stod(n);
}
catch(std::invalid_argument)
{
    // can't convert
}
Chimera
  • 5,884
  • 7
  • 49
  • 81