setiosflags
来源:互联网 发布:股东人数查看软件 编辑:程序博客网 时间:2024/06/07 17:48
本课讲:setiosflags( ios::fixed ),其头文件为:include<iomanip>.
注:在遇到要计算浮点数且希望能控制其输出、精度、小数点后的位数等时,那么这个时候,用setiosflags( ios::fixed )来控制是再好不过了!且看下面程序:
#include<iostream>
#include<cmath>
using namespace std;
int main() {
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
}
输出结果为:sqrt(2000) = 44.7214. 那么也就是说编译器的默认精度为小数点后4位。那么如果我想让其小数点后精度为1位、2位、3位或20位,该怎么办呢?来,这么试试:
例一:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
cout << setprecision( 1 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} //结果为4e+001
例二:
int main() {
cout << setprecision( 1 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} //结果为45
例三:
int main() {
cout << setprecision( 3 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} //结果为44.7
大家可以明显看到,编译器输出的结果纯粹是无稽之谈!根本不是按照人们的意志去做的!遇到这种比艳门照还尴尬的情况该怎么办呢?不要怕,这时候动动脑子,救星就来啦!它就是人称一朵犁花压海棠的:setiosflags( ios::fixed ) ! 废话少说,拿实例来!
例一:
int main() {
cout << setiosflags( ios::fixed ) << setprecision( 1 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} //结果为44.7
例二:
int main() {
cout << setiosflags( ios::fixed ) << setprecision( 2 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} //结果为44.72
例三:
int main() {
cout << setiosflags( ios::fixed ) << setprecision( 3 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} //结果为44.721
Apparently, all of the answers are totally correct with any doubt! 结果全部正确,哪怕你来个 setiosflags( ios::fixed ) << setprecision( 1000 ), 结果照样不来半点寒糊!
此外,还要补充一点的是,某些人,当吃饱撑得在床上直打滚儿、嗷嗷叫着难受时,便喜欢多惹出点事非来,他们喜欢在setiosflags()里再加个showpoint,我不知道这样做好是不好,可我感觉它确实有点多余!因为加不加showpoint几乎是没什么区别,如果你也吃多了,肚子涨,胃痛胃酸不消化,非要挑骨头捡刺儿,那也能找出点不一样,就是:当setprecision()的精度为0的时候,你有showpoint,那结果你就会多个点儿,没showpoint,就没有。实例:
int main() {
cout << setiosflags( ios::fixed ) << setprecision( 0 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} // 结果 45
int main() {
cout << setiosflags( ios::fixed|ios::showpoint ) << setprecision( 0 );
cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;
return 0;
} //结果 45.
看到这个点儿没?所以说嘛,showpoint,可有可无!如果想简化程序,那干脆就不加!
最后,再次特别提醒大家,想使用setiosflags或者setprecision时,一定得加头文件<iomanip>,在setiosflags()里面填东西的时候,前面一定得加上域符 ios::。 好啦,这次就是这样,下回再说!
- setiosflags
- setiosflags作用
- setprecision()与setiosflags()
- setiosflags() 的好处多多
- setiosflags的问题
- setiosflags的一些用法
- setiosflags() 的好处多多
- setprecision和setiosflags函数
- setiosflags的问题
- setiosflags的一些用法
- setiosflags() 的好处多多
- setpercision(n) setiosflags(ios::fixed) setiosflags(ios::scientific)
- setprecision 和 setiosflags(ios::fixed)
- C++中setiosflags()的用法
- C++ 格式控制符setiosflags
- setiosflags(ios::fixed)和setprecision()
- C++中setiosflags()的用法
- manipulator and setiosflags and limit width
- Oracle中复制表
- java:File常用基础操作
- POJ 3173 Parkside's Triangle 可能会
- v-if绑定的元素为什么事件没有响应
- cvLoadImage函数使用方法
- setiosflags
- 20170226 BUG ANN相关的编程问题
- iOS中 Animation 动画大全 韩俊强的博客
- 算法 第二章--快速排序
- spring4.x注解
- 187. Repeated DNA Sequences
- 修改织梦dedecms自带网站地图路径和获取文章列表支持标签调用的方法
- 明天又要开始上班啦
- Codeforces 777E(贪心+单调栈)