一:setprecision 作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入。 比如:
[C++] 纯文本查看 复制代码 double s=20.7843000,
cout<<setprecision(1)<<s<<endl;//会输出2e+001,因为要输出一个数字,所以只有2.
cout<<setprecision(2)<<s<<endl;//会输出21。
cout<<setprecision(3)<<s<<endl;//会输出20.8。
cout<<setprecision(6)<<s<<endl;//会输出20.7843。
cout<<setprecision(7)<<s<<endl;//会输出20.7843。
cout<<setprecision(8)<<s<<endl;//会输出20.7843。
可见,小数部分末尾为0时,是输不出来的! 要想输出来,就得用showpoint了。 特别提示 : (如果再在这些语句后面加个两个语句:
[C++] 纯文本查看 复制代码 cout<<1<<endl;
cout<<1.00800<<endl;
猜到会输出什么吗? 第一条输出:1。不是浮点型。 第二条为:1.008。承接setprecision(8)的这条规则语句。 注: 如果直接有语句
[C++] 纯文本查看 复制代码 int main()
{
cout<<1<<endl;
cout<<1.00<<endl;
}
第一条输出:1。 第二条也为:1。按整型输出
) 二:setprecision与showpoint 默认情况下,浮点数不会显示尾数 0,并且如果没有小数部分的浮点数则不显示小数点。若想要输出,需要用showpoint。
当使用 showpoint 时,表示打印浮点数的小数点和小数位数,即使显示的数值没有小数点。 语法:在输出语句前声明:cout.setf(ios::showpoint);就行了! 还比如:
[C++] 纯文本查看 复制代码 double s=20.7843000,
cout.setf(ios::showpoint);
cout<<setprecision(1)<<s<<endl; //就会输出2.e+001,注意,2和e之间多了一个“.”。
cout<<setprecision(2)<<s<<endl; //会输出21.。多个点!
cout<<setprecision(3)<<s<<endl; //会输出20.8。
cout<<setprecision(6)<<s<<endl; //会输出20.7843。
cout<<setprecision(7)<<s<<endl; //会输出20.78430。
cout<<setprecision(8)<<s<<endl; //会输出20.784300。
可见,就会输出想要的数据数目! 特别提示 : (如果再在这些语句后面加个两个语句:
[C++] 纯文本查看 复制代码 cout<<1<<endl;
cout<<1.0080<<endl;
猜到会输出什么吗? 第一条输出:1。不是浮点型。 第二条也为:1.0080000。承接setprecision(8)的这条规则语句。
三:setprecision与fixed 如果想要保留几位小数,那setprecision就得与fixed合作了!! 语法:在输出语句前声明:cout.setf(ios::fixed); 比如:
[C++] 纯文本查看 复制代码 double s=20.7843909
cout.setf(ios::fixed);
cout<<setprecision(1)<<s<<endl; //就会输出2.8 。
cout<<setprecision(2)<<s<<endl; //会输出21.78。多个点!
cout<<setprecision(3)<<s<<endl; //会输出20.784。
cout<<setprecision(6)<<s<<endl; //会输出20.784391。
cout<<setprecision(7)<<s<<endl; //会输出20.7843909。
cout<<setprecision(8)<<s<<endl; //会输出20.78439090。
特别提示 : (如果也再在这些语句后面加个两个语句:
[C++] 纯文本查看 复制代码 cout<<1<<endl;
cout<<1.008<<endl;
猜到会输出什么吗? 第一条输出:1。 第二条为:1.00800000。 就是承接了setprecision(8)的这条规则语句,是浮点型的都会保留8个小数。是整型的还是整型!) 语句也可以写成:cout<<fixed<<setprecision(2)<<s<<endl; 就算后面的语句没有写<<fixed,同样会按有<<fixed处理。
比如有语句: cout<<fixed<<setprecision(2)<<s<<endl; A:cout<<setprecision(7)<<s<<endl; B:cout<<setprecision(8)<<s<<endl; AB语句均会按保留7个,8个小数处理,不会再按有7或8个浮点数处理。 如果下面有语句c:
cout<<1.008<<endl;也会保留8个小数。
四:setprecision、showpoint与fixed
[C++] 纯文本查看 复制代码 cout<<fixed<<setprecision(2)<<123.456<<endl; //输出的结果是123.46
cout<<showpoint<<12345.0006666<<endl; //输出12345.0
cout<<fixed<<setprecision(2)<<123.456<<endl;
比如:
[C++] 纯文本查看 复制代码 double s=20.7843909
cout<<setprecision(2)<<s<<endl;//输出21
cout<<fixed<<s<<endl;//输出20.78
cout<<setprecision(2)<<s<<endl;//输出21
cout<<showpoint<<s<<endl;//输出21.(有个点)
cout<<fixed<<s<<endl;//输出20.78391
cout<<showpoint<<s<<endl;//输出20.78391
cout<<setprecision(2)<<s<<endl;//输出21
cout<<fixed<<s<<endl;//输出20.78
cout<<showpoint<<s<<endl;//输出20.78
cout<<setprecision(2)<<s<<endl;//输出21
cout<<showpoint<<s<<endl;//21.(有个点)
cout<<fixed<<s<<endl;//20.78
setfill与setw 这两个函数都在iosmanip头文件中。 setw()用于设置输出数据的最小域宽,当输出的数据不足所设置的域宽时,默认会在数据前补空格。 若想要指定数据长度不足域宽时的填补字符,就需要用到setfill函数:cout << setfill(' '); setfill与setw常搭配使用:
[C++] 纯文本查看 复制代码 cout << setfill('0') << setw(3) << num; //实现数字位数不够3位时,输出时前面补0
//也可直接写为:printf("%03d", num);
|