can't print more decimal of pi [duplicate]

雨燕双飞 提交于 2019-12-20 04:13:19

问题


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?


回答1:


Like so:

#include <iomanip>
#include <iostream>

std::cout << std::setw(15) << pi_18 << std::endl;

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.




回答2:


You could use the precision method:

cout.precision(15);

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



来源:https://stackoverflow.com/questions/18969832/cant-print-more-decimal-of-pi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!