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的用法总结(经典!!超经典!!)
首先要加头文件:iomanip 一:setprecision 作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入. 比如:double s=20.784 ...
- setprecision、fixed、showpoint的用法总结(经典!!超经典!!)【转】
本文转载自:http://blog.csdn.net/u011321546/article/details/9293547 首先要加头文件:iomanip 一:setprecision 作用:控制输出 ...
- 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 ...
- CSS中position:fixed的相关用法
CSS中的三大重点知识: 1.float,浮动 2.盒子模型 3.position绝对定位 今天主要写下position中fixed相关知识: position:static,relative,abs ...
- STL之sstream的用法
STL之sstream的用法 说在前面: 库定义了三种类:istringstream.ostringstream和stringstream,分别用来进行流的输入.输出和输入输出操作.另外,每个类都有一 ...
- poj3122-Pie(二分法+贪心思想)
一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...
- C++编程学习(九)this指针&友元函数
mooc西工大魏英老师的课程通道关闭了,难受.现在边看工程代码边重温刷第一遍C++时候的知识点,顺序没有按照大纲的来,想到哪写到哪. this是干啥用的? 简介:在 C++ 中,每一个对象都能通过 t ...
随机推荐
- hdu 5384 Danganronpa
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 思路:没学自动机时以为是道KMP然后就tle了好几把,AC自动机模板题 #include<cs ...
- llvm-summary
llvm 学习总结 Type define int类型 IntegerType::get(mod->getContext(), 32) long类型 IntegerType::get(mod-& ...
- HP-SOCKET TCP/UDP通信框架库解析
项目概述: HP-SOCKET是一套通用TCP/UDP通信框架,包括服务器.客户端.Agent组件:其目标是提供高性能.通用性.简易性.可扩展.可定制: 鉴于此,其仅实现基本的通用框架通信.数据收发功 ...
- CI中的数据库操作
转载于:http://blog.sina.com.cn/s/blog_76e7bdba01016p2p.html CI中第一次连接数据库,在控制器或模型的构造函数里输入以下语句 $this->l ...
- JAVA中MAP值保持顺序不变
今天在进行JAVA开发过程中,因需要使用MAP来存放数据,同时希望MAP中KEY的顺序与放入顺序保持一致. 在使用HashMap之后,发现KEY的顺序是乱序的,每次打印还不太一样.上网查询资料之后发现 ...
- mysql基本信息收集
1.下载安装 percona-toolkit 工具包http://www.percona.com/downloads/percona-toolkit/LATEST/tarball/2.运行下面两个工具 ...
- Unable to convert MySQL date/time value to System.DateTime 错误
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
- POJ 1743 Musical Theme ——后缀数组
[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include ...
- PD PDM模型中关系设置为概念模型样式
来自为知笔记(Wiz)
- 可变参数列表与printf()函数的实现
问题 当我们刚开始学习C语言的时候,就接触到printf()函数,可是当时"道行"不深或许不够细心留意,又或者我们理所当然地认为库函数规定这样就是这样,没有发现这个函数与普通的函数 ...