本文转载自:http://blog.csdn.net/u011321546/article/details/9293547

首先要加头文件: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的用法总结(经典!!超经典!!)【转】的更多相关文章

  1. setprecision、fixed、showpoint的用法总结(经典!!超经典!!)

    首先要加头文件:iomanip 一:setprecision 作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入. 比如:double s=20.784 ...

  2. setprecision、fixed、showpoint的用法总结

    首先要加头文件:iomanip 一:setprecision 作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入. 比如:double s=20.784 ...

  3. css中table-layout:fixed 属性的用法

    table-layout:fixed 属性的用法:如果想要一个table固定大小,里面的文字强制换行(尤其是在一长串英文文本,并且中间无空格分隔的情况下),以达到使过长的文字 不撑破表格的目的,一般是 ...

  4. setprecision **fixed

    #include <iostream> #include <iomanip> using namespace std; int main( void ) { const dou ...

  5. C / C++ 保留小数函数(setprecision(n)的一些用法总结)

    从C语言开始正式学习C++,但是一上来输出位数就懵了,查资料才知道C++需要使用 “ setprecision  ”函数.自己总结一下. 首先说C++代码 #include <iomanip&g ...

  6. 王家林 大数据Spark超经典视频链接全集[转]

    压缩过的大数据Spark蘑菇云行动前置课程视频百度云分享链接 链接:http://pan.baidu.com/s/1cFqjQu SCALA专辑 Scala深入浅出经典视频 链接:http://pan ...

  7. CSS中position:fixed的相关用法

    CSS中的三大重点知识: 1.float,浮动 2.盒子模型 3.position绝对定位 今天主要写下position中fixed相关知识: position:static,relative,abs ...

  8. 超经典~超全的jQuery插件大全

    海量的jQuery插件帖,很经典,不知道什么时候开始流传,很早以前就收藏过,为了工作方便还是发了一份放在日志里面. 其中有些已经无法访问,或许是文件移除,或许是被封锁.大家分享的东西,没什么特别的可说 ...

  9. 一个超经典 WinForm 卡死问题的再反思

    一:背景 1.讲故事 这篇文章起源于昨天的一位朋友发给我的dump文件,说它的程序出现了卡死,看了下程序的主线程栈,居然又碰到了 OnUserPreferenceChanged 导致的挂死问题,真的是 ...

随机推荐

  1. [转帖]c++ 面试整理

    1. 继承方式 public    父类的访问级别不变 protected    父类的public成员在派生类编程protected,其余的不变 private        父类的所有成员变成pr ...

  2. 01--TCP状态转换

    参考大牛文章: http://www.cnblogs.com/qlee/archive/2011/07/12/2104089.html

  3. 基于openstack平台的几种Cloud DB解决方案

    方案一.openstack 官方 trove解决方案 此方案进行过镜像的打包,由于网络问题,还未能成功实现 方案二.salt 或者ansible+ docker 由于 docker部署数据库,在数据库 ...

  4. Android Studio 快捷键整理

    Alt+回车 导入包,自动修正 Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如ge ...

  5. 64位windows系统如何显示32位dcom组件配置

    在运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,但是却发现找不到Microsoft Excel程序,这主要是64位系统的问题,excel是32位的组件,所以在正常的系统组件服务里是看不到的 ...

  6. 10、scala面向对象编程之Trait

    1.  将trait作为接口使用 2.trait中定义具体方法 3.trait定义具体字段 4.trait中定义抽象字段 5.为实例对象混入trait 6.trait调用链 7.在trait中覆盖抽象 ...

  7. python笔记之发送邮件

    发送邮件前提:开启邮箱授权码 一.开启授权码(以163邮箱为例) 1.登录163邮箱,点击设置--POP3/SMTP/IMAP,出现设置界面   2. 开启SMTP服务且可以查询SMTP的host地址 ...

  8. seam remote 返回的map结构

    map结构的数据,js接收到的结构是elements下面的一个 [ {key:***,value:***}, {key:***,value:***} ] 这样子的集合,需要经过下面代码的转换才能重新变 ...

  9. C/C++ 之数组排序

    #include <stdio.h> #include <stdlib.h> void array_sort(int *a, int len) { int i, j, tmp; ...

  10. CentOS7阿里云服务器,python程序requests无法正常post网站(报502)

    问题描述: 使用jenkins构建接口自动化测试时,发现新增加的接口case不能访问通,会报502错误(本地可以跑通,在测试服就会502)解决的思路: 缩小调试范围(去掉jenkins db环境,将问 ...