Baldrick
Baldrick

Reputation: 11012

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

Upvotes: 2

Views: 275

Answers (3)

zeller
zeller

Reputation: 4974

That is because this is the default formatting, you can override it with precision.

Upvotes: 2

Samy Arous
Samy Arous

Reputation: 6814

Try this:

using std::fixed;
...
ss.setf(fixed);
ss << myDouble;
...

Upvotes: 2

Bartosz Przybylski
Bartosz Przybylski

Reputation: 1656

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;
}

Upvotes: 2

Related Questions