Weket Lava
Weket Lava

Reputation: 31

"Double" is not printing more than 6 significant digits even after setprecision

I'm self learning C++ and for some reason "double" doesn't print more than 6 significant digits even after std::setprecision. Do I need to do something else? Most recent version of codeblocks if that helps. This is all the code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    std::setprecision(9);
    double A = 654321.987;
    cout << A << endl;
    return 0;
}

Upvotes: 0

Views: 759

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263497

You need to feed the result of std::setprecision(9) to std::cout. Otherwise it has no way of knowing what output stream it applies to (and so it won't apply to anything).

std::cout << std::setprecision(9) << A << std::endl;

Or if you prefer you can do it separately:

std::cout << std::setprecision(9);
std::cout << A << std::endl;

Upvotes: 5

Related Questions