在c ++中小数点后显示两位数

DSK*_*KVP 23 c++ precision

论坛已经讨论过类似的话题.但是我在下面的代码中有一些不同的问题:

double total;
cin >> total;
cout << fixed << setprecision(2) << total;
Run Code Online (Sandbox Code Playgroud)

如果我提供输入,100.00那么程序打印只是100但不是100.00

我怎么打印100.00

Arm*_*yan 60

cout << fixed << setprecision(2) << total;
Run Code Online (Sandbox Code Playgroud)

setprecision指定最小精度.所以

cout << setprecision (2) << 1.2; 
Run Code Online (Sandbox Code Playgroud)

将打印1.2

fixed 说小数点后面会有一个固定的小数位数

cout << setprecision (2) << fixed << 1.2;
Run Code Online (Sandbox Code Playgroud)

将打印1.20


小智 5

可以使用以下命令在 C++ 中打印 15 位十进制数:

#include <iomanip>
#include <iostream>

cout << fixed << setprecision(15) << " The Real_Pi is: " << real_pi << endl;
cout << fixed << setprecision(15) << " My Result_Pi is: " << my_pi << endl;
cout << fixed << setprecision(15) << " Processing error is: " << Error_of_Computing << endl;
cout << fixed << setprecision(15) << " Processing time is: " << End_Time-Start_Time << endl;
_getch();

return 0;
Run Code Online (Sandbox Code Playgroud)


woo*_*.as 1

这可以通过 setiosflags(ios::showpoint) 来实现。