C++如何保留两位有效数字!!!!
#include <iostream> #include
#include <iomanip> //注意这个头文件 #include
//注意这个头文件
using namespace std; using namespace std;
int main() int main()
{ {
double t = 2.1356899889898; double t = 2.1356899889898;
cout.precision(2); //第一种方法 cout.precision(2); //第一种方法
cout.setf(ios::fixed); cout.setf(ios::fixed);
cout << t << endl; cout << t << endl;
cout << setprecision(2) << fixed << t << endl; //第二种方法 cout << setprecision(2) << fixed << t << endl; //第二种方法
cout << setiosflags(ios::fixed) << setprecision(2) << t << endl; //第三种方法 cout << setiosflags(ios::fixed) << setprecision(2) << t << endl; //第三种方法
system("pause"); system("pause");
return 0; return 0;
} }
不过他的运行结果会自动四舍五入 不过他的运行结果会自动四舍五入
第一种方法不需要加那个头文件,剩余两个都要加那个头文件。 第一种方法不需要加那个头文件,剩余两个都要加那个头文件。
感觉写得好的, 感觉写得好的,
