C++ 如何在输出控制台中显示更多小数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1231685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:14:39  来源:igfitidea点击:

How do I display more decimals in the output console?

c++console

提问by Faken

I want to output the value of a double in it's full precision. However, when using the cout function, it only displays the first 6 digits even though there is around 15-16 digits of precision.

我想以全精度输出双精度值。然而,当使用 cout 函数时,它只显示前 6 位数字,即使有大约 15-16 位的精度。

How do I get my program to display the entire value, including the magnitude (power) component?

如何让我的程序显示整个值,包括幅度(功率)分量?

回答by Amber

Use the setprecision()manipulator:

使用setprecision()操纵器:

http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

You can also force scientific notation with the scientificmanipulator:

您还可以使用scientific操纵器强制使用科学记数法:

http://www.cplusplus.com/reference/iostream/manipulators/scientific/

http://www.cplusplus.com/reference/iostream/manipulators/scientific/

cout << scientific << setprecision(15) << my_number << endl;

回答by Indy9000

you could use something like this :

你可以使用这样的东西:

#include <iomanip>

cout << setprecision (9) << double_value << endl;

more iomanipulators, here

更多的iomanipulators,在这里

回答by Sam Harwell

You're looking for setprecision(code taken from link):

您正在寻找setprecision(来自链接的代码):

int main () {
  double f =3.14159;
  cout << setprecision(15) << f << endl;
  return 0;
}