小数点后几位和几位有效数字

 cout << setiosflags(ios::fixed) << setpresition(6) <<   a << endl;  为输出a这个变量的六位小数

cout << setprecision(3) << a << endl;为输出a这个变量的3位有效数字

 

#include <iostream>
#include <iomanip>//对于保留几位小数,需要头文件#include <iomanip>
#include <stdio.h>
using namespace std;
int main(){
double n;
cout <<"请输入要测试的数字" << endl;
cin >> n;
//保留6位有效数字,前提是输入的为带精度的数字,例如5.5555666
cout << setprecision(6) << n << endl;
cout << setiosflags(ios::fixed) <<setprecision(3) << n << endl; //控制保留三位小数,不够0填充
}
 

 



posted @ 2020-07-18 21:14  Tomorrow1126  阅读(796)  评论(0编辑  收藏  举报