0

The last decimal value is not converted to double and does not appear in the print. How to fix this?

#include <iostream>

using namespace std;

double toDouble(const char* str)
{
   return stod(str);
}

int main()
{
    string text = "10158.34";
    double value = toDouble(text.c_str());
    cout << value << endl;

    return 0;
} 

Thanks.

KernelPanic
  • 2,328
  • 7
  • 47
  • 90

1 Answers1

0

The default output setting is to print six significant digits; see std::setprecision.

Example:

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

int main()
{
    std::string text = "10158.34";
    double value = std::stod(text);
    std::cout << std::setprecision(100) << value << std::endl;
} 

which prints 10158.34000000000014551915228366851806640625, because 10158.34 can't be represented exactly as a double.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82