找回密码
 中文实名注册
查看: 161|回复: 0

setprecision、fixed、showpoint的用法总结

[复制链接]

694

主题

1082

帖子

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
22801
发表于 2022-7-1 08:32:21 | 显示全部楼层 |阅读模式
首先要加头文件iomanip
一: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

setfillsetw
这两个函数都在iosmanip头文件中。
setw()用于设置输出数据的最小域宽,当输出的数据不足所设置的域宽时,默认会在数据前补空格。
若想要指定数据长度不足域宽时的填补字符,就需要用到setfill函数:cout << setfill(' ');
setfillsetw常搭配使用:

[C++] 纯文本查看 复制代码
cout << setfill('0') << setw(3) << num; //实现数字位数不够3位时,输出时前面补0
//也可直接写为:printf("%03d", num);


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2024-4-30 04:14 , Processed in 0.041442 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表