setprecision、fixed、showpoint的用法总结(经典!!超经典!!)
首先要加头文件:iomanip
一:setprecision
作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入。
比如: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了。
特别提示:
(如果再在这些语句后面加个两个语句:
cout<<1<<endl;
cout<<1.00800<<endl;
猜到会输出什么吗?
第一条输出:1。不是浮点型。
第二条为:1.008。承接setprecision(8)的这条规则语句。
注:
如果直接有语句
int main()
{
cout<<1<<endl;
cout<<1.00<<endl;
}
第一条输出:1。
第二条也为:1。按整型输出
)
二:setprecision与showpoint
语法:在输出语句前声明:cout.setf(ios::showpoint);就行了!
还比如: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。
可见,就会输出想要的数据数目!
特别提示:
(如果再在这些语句后面加个两个语句:
cout<<1<<endl;
cout<<1.0080<<endl;
猜到会输出什么吗?
第一条输出:1。不是浮点型。
第二条也为:1.0080000。承接setprecision(8)的这条规则语句。
三:setprecision与fixed
如果想要保留几位小数,那setprecision就得与fixed合作了!!
语法:在输出语句前声明:cout.setf(ios::fixed);
比如: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。
特别提示:
(如果也再在这些语句后面加个两个语句:
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
{cout<<fixed<<setprecision(2)<<123.456<<endl;//输出的结果是123.46
cout<<showpoint<<12345.0006666<<endl;//输出12345.0
cout<<fixed<<setprecision(2)<<123.456<<endl;}
比如:double s=20.7843909
1.有语句
cout<<setprecision(2)<<s<<endl;//输出21
cout<<fixed<<s<<endl;//输出20.78
2.有语句:
cout<<setprecision(2)<<s<<endl;//输出21
cout<<showpoint<<s<<endl;//输出21.(有个点)
3.有语句:
cout<<fixed<<s<<endl;//输出20.78391
cout<<showpoint<<s<<endl;//输出20.78391
4.有语句:
cout<<setprecision(2)<<s<<endl;//输出21
cout<<fixed<<s<<endl;//输出20.78
cout<<showpoint<<s<<endl;//输出20.78
5.有语句:
cout<<setprecision(2)<<s<<endl;//输出21
cout<<showpoint<<s<<endl;//21.(有个点)
cout<<fixed<<s<<endl;//20.78
setprecision、fixed、showpoint的用法总结(经典!!超经典!!)的更多相关文章
- setprecision、fixed、showpoint的用法总结(经典!!超经典!!)【转】
本文转载自:http://blog.csdn.net/u011321546/article/details/9293547 首先要加头文件:iomanip 一:setprecision 作用:控制输出 ...
- setprecision、fixed、showpoint的用法总结
首先要加头文件:iomanip 一:setprecision 作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入. 比如:double s=20.784 ...
- css中table-layout:fixed 属性的用法
table-layout:fixed 属性的用法:如果想要一个table固定大小,里面的文字强制换行(尤其是在一长串英文文本,并且中间无空格分隔的情况下),以达到使过长的文字 不撑破表格的目的,一般是 ...
- setprecision **fixed
#include <iostream> #include <iomanip> using namespace std; int main( void ) { const dou ...
- C / C++ 保留小数函数(setprecision(n)的一些用法总结)
从C语言开始正式学习C++,但是一上来输出位数就懵了,查资料才知道C++需要使用 “ setprecision ”函数.自己总结一下. 首先说C++代码 #include <iomanip&g ...
- 王家林 大数据Spark超经典视频链接全集[转]
压缩过的大数据Spark蘑菇云行动前置课程视频百度云分享链接 链接:http://pan.baidu.com/s/1cFqjQu SCALA专辑 Scala深入浅出经典视频 链接:http://pan ...
- CSS中position:fixed的相关用法
CSS中的三大重点知识: 1.float,浮动 2.盒子模型 3.position绝对定位 今天主要写下position中fixed相关知识: position:static,relative,abs ...
- 超经典~超全的jQuery插件大全
海量的jQuery插件帖,很经典,不知道什么时候开始流传,很早以前就收藏过,为了工作方便还是发了一份放在日志里面. 其中有些已经无法访问,或许是文件移除,或许是被封锁.大家分享的东西,没什么特别的可说 ...
- 一个超经典 WinForm 卡死问题的再反思
一:背景 1.讲故事 这篇文章起源于昨天的一位朋友发给我的dump文件,说它的程序出现了卡死,看了下程序的主线程栈,居然又碰到了 OnUserPreferenceChanged 导致的挂死问题,真的是 ...
随机推荐
- java中HashSet详解
HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...
- MyEclipse 设置全部jsp的编码为UFT-8 的方法
- 转载JQuery绑定鼠标粘贴事件工具类
// 粘贴事件监控 $.fn.pasteEvents = function( delay ) { if (delay == undefined) delay = 10; return $(this). ...
- or1200于IMMU分析
以下摘录<步骤吓得核心--软-core处理器的室内设计与分析>一本书 1 IMMU结构 OR1200中实现IMMU的文件有or1200_immu_top.v.or1200_immu_tlb ...
- MySQL安装指南
近期领导突然说要用MySQL,我立刻当天晚上就研究了一下. http://www.mysql.com/这是官网,还好能够訪问.好多年前已经被oracle收购.分为企业版和社区版: MySQL Ente ...
- NET Socket服务编程
smark https://github.com/IKende/ .NET Socket服务编程之-高效连接接入编 在.NET上编写网络服务深入都有2,3年了,而这些时间时如何在.NET里实现网络服务 ...
- 2.3 LINQ查询表达式中 使用select子句 指定目标数据
本篇讲解LINQ查询的三种形式: 查询对象 自定义查询对象某个属性 查询匿名类型结果 [1.查询结果返回集合元素] 在LINQ查询中,select子句和from子句都是必备子句.LINQ查询表达式必须 ...
- leetcode第15题--3Sum
Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...
- Mysql 使用 select into outfile
Mysql支持将查询结果到处 默认语法 select .. from table into outfile "filepath\filename.txt"; 如果在执行的过程中遇 ...
- readonly和const的区别
readonly与const的区别1.const常量在声明的同时必须赋值,readonly在声明时可以不赋值2.readonly只能在声明时或在构造方法中赋值(readonly的成员变量可以根据调用不 ...