#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
double a = 12.3456789111;
double b = 123.456789111;
cout<<"默认是六位有效数字,同时还四舍五入:"<<endl;
cout<<a<<endl;
cout<<b<<endl;
cout<<"---------------------------------------------------"<<endl;
cout<<"用这个cout.precision(2) 来改变保留几位有效"<<endl<<endl;
cout.precision(2); //用这个来改变默认保留几位小数
cout<<"保留两位有效:"<<endl;
cout<<a<<endl;
cout.precision(4);
cout<<"保留四位有效:"<<endl;
cout<<a<<endl;
cout.precision(8);
cout<<"保留八位有效:"<<endl;
cout<<a<<endl;
cout<<"---------------------------------------------------"<<endl;
cout<<"定点法:就是保留几位小数,not有效,用这个cout.flags(cout.fixed);"<<endl<<endl;
cout.precision(3);
cout.flags(cout.fixed);//定点法
cout<<"3位小数:"<<endl;
cout<<a<<endl;
cout.precision(5);
cout<<"5位小数:"<<endl;
cout<<a<<endl;
cout<<"----------------------------------------------------"<<endl;
cout<<"取消定点法:用cout.unsetf(cout.fixed);"<<endl<<endl;
cout.unsetf(cout.fixed);//取消定点法
cout<<"5位有效,已经取消的定点法:"<<endl;
cout<<a<<endl;
}
cout输出精度问题
最新推荐文章于 2023-10-25 09:20:30 发布