Search code examples
c++stringdouble-precision

c++ String is shortening double when printing


This string operation prints out a double in short-hand, and I can't work out why. Why is this happening, and how can I get the full output like the first line of output?

string myString = "The value is ";
ss.str(""); // stringstream from ealier
ss.clear();
ss << myDouble; // Double with value 0.000014577
myString.append(ss.str());
cout << myDouble << endl;
cout << myString << endl;

$ ./myapp
0.000014577
The value is 1.4577e-05

Solution

  • its default behaviour you should use precision to use fixed precision

    #include <string>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main() {
    double v = 0.000014577;
    cout << fixed << v << endl;
    }