This question already has an answer here:
I have tried to use long double type in my program to print out more digits of pi. But it only shows 5 digits decimal.
Here is my code.
int main(int argc, char** argv) {
long double pi_18 = acos(static_cast<long double>(-1));
cout << "pi to 18:" << pi_18 << endl;
return 0;
}
and this is my output:
pi to 18: 3.14159
How can I fix this problem?
Like so:
The width modifier only affects the next formatting operation, so if you want to format multiple numbers, you have to repeat it before every one. Check out the full documentation of format specifiers.
You could use the precision method:
This allows you to define the precision only once. You don't have to repeat it like with std::setw()
For more information see: http://en.cppreference.com/w/cpp/io/ios_base/precision