C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢。。。?
下面的方法是在网上找到的,如果各位有别的办法谢谢留下...

iomanip.h是I/O流控制头文件,就像C里面的格式化输出一样.以下是一些常的:

dec 置基数为10 相当于"%d"
hex 置基数为16 相当于"%X"
oct 置基数为8 相当于"%o"
setfill(c) 设填充字符为c
setprecision(n)   设显示小数精度为n位
setw(n) 设域宽为n个字符
setioflags(ios::fixed)   固定的浮点显示
setioflags(ios::scientific) 指数表示
setiosflags(ios::left) 左对齐
setiosflags(ios::right) 右对齐
setiosflags(ios::skipws 忽略前导空白
setiosflags(ios::uppercase) 16进制数大写输出
setiosflags(ios::lowercase) 16进制小写输出
setiosflags(ios::showpoint) 强制显示小数点
setiosflags(ios::showpos)   强制显示符号

#include <iomanip>
use namespace std;

double d=11.23456;
cout<<d<<endl;                                                  //直接输出只能输出6位数,包括整数部分和小数部分
cout<<setprecision(3)<<d<<endl;               //精度为3,输出3位数
cout<<setiosflags(ios::fixed)<<d<<endl;//精度为3,定点输出,输出3位小数
cout<<setiosflags(ios::fixed)<<setprecision(7)<<d<<endl;//位数不够,末尾添0

输出结果:
11.2346
11.2
11.23456
11.2345600

C++格式化输出浮点数

view plaincopy to clipboardprint?
01.#include <iostream>  
02.using std::cout;  
03.using std::endl;  
04.using std::fixed;  
05.using std::scientific;  
06. 
07.int main()  
08.{  
09.   double x = 0.001234567;  
10.   double y = 1.946e9;  
11. 
12.   cout << "Displayed in default format:" << endl << x << '\t' << y << endl;  
13. 
14.   cout << "\nDisplayed in scientific format:" << endl << scientific << x << '\t' << y << endl;  
15. 
16.   cout << "\nDisplayed in fixed format:" << endl << fixed << x << '\t' << y << endl;  
17.   return 0;  
18.} 
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
using std::scientific;

int main()
{
   double x = 0.001234567;
   double y = 1.946e9;

cout << "Displayed in default format:" << endl << x << '\t' << y << endl;

cout << "\nDisplayed in scientific format:" << endl << scientific << x << '\t' << y << endl;

cout << "\nDisplayed in fixed format:" << endl << fixed << x << '\t' << y << endl;
   return 0;
}

Displayed in default format:
0.00123457      1.946e+009

Displayed in scientific format:
1.234567e-003   1.946000e+009

Displayed in fixed format:
0.001235        1946000000.000000

view plaincopy to clipboardprint?
01.#include <iostream.h>  
02. 
03.main(void)  
04.{  
05.  float a=100100.0, b=0.08;  
06.  cout.setf(ios::right|ios::scientific|ios::showpoint);  
07.  cout.width(20);     
08.  cout <<(-a*b);  
09.    
10.  return 0;  
11.} 
#include <iostream.h>

main(void)
{
  float a=100100.0, b=0.08;
  cout.setf(ios::right|ios::scientific|ios::showpoint);
  cout.width(20);  
  cout <<(-a*b);
  return 0;
}

-8.008000e+003

view plaincopy to clipboardprint?
01.#include <iostream>  
02.#include <iomanip>  
03.#include <limits>  
04.using std::cout;  
05.using std::endl;  
06.using std::setprecision;  
07.using std::numeric_limits;  
08. 
09.int main() {  
10.  const double pi = 3.14;  
11.  cout << endl;  
12. 
13.  for(double radius = .2 ; radius <= 3.0 ; radius += .2)  
14.    cout << "radius = " 
15.    << setprecision(numeric_limits<double>::digits10 + 1)  
16.    << std::scientific << radius<< "  area = " 
17.         << std::setw(10) << setprecision(6)<< std::fixed << pi * radius * radi  
18.us << endl;  
19.  return 0;  
20.} 
#include <iostream>
#include <iomanip>
#include <limits>
using std::cout;
using std::endl;
using std::setprecision;
using std::numeric_limits;

int main() {
  const double pi = 3.14;
  cout << endl;

for(double radius = .2 ; radius <= 3.0 ; radius += .2)
    cout << "radius = "
    << setprecision(numeric_limits<double>::digits10 + 1)
    << std::scientific << radius<< "  area = "
         << std::setw(10) << setprecision(6)<< std::fixed << pi * radius * radi
us << endl;
  return 0;
}

radius = 2.0000000000000001e-001  area =   0.125600
radius = 4.0000000000000002e-001  area =   0.502400
radius = 6.0000000000000009e-001  area =   1.130400
radius = 8.0000000000000004e-001  area =   2.009600
radius = 1.0000000000000000e+000  area =   3.140000
radius = 1.2000000000000000e+000  area =   4.521600
radius = 1.3999999999999999e+000  area =   6.154400
radius = 1.5999999999999999e+000  area =   8.038400
radius = 1.7999999999999998e+000  area =  10.173600
radius = 1.9999999999999998e+000  area =  12.560000
radius = 2.1999999999999997e+000  area =  15.197600
radius = 2.3999999999999999e+000  area =  18.086400
radius = 2.6000000000000001e+000  area =  21.226400
radius = 2.8000000000000003e+000  area =  24.617600

view plaincopy to clipboardprint?
01.#include <iostream>  
02.#include <iomanip>  
03.#include <string>  
04. 
05.using namespace std;  
06. 
07.int main( ) {  
08. 
09.   ios_base::fmtflags flags = cout.flags( );  
10. 
11.   double pi = 3.14285714;  
12. 
13.   cout << "pi = " << setprecision(5) << pi << '\n';  
14. 
15.   cout.flags(flags);  
16.} 
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main( ) {

ios_base::fmtflags flags = cout.flags( );

double pi = 3.14285714;

cout << "pi = " << setprecision(5) << pi << '\n';

cout.flags(flags);
}

pi = 3.1429

view plaincopy to clipboardprint?
01.#include <iostream>  
02.#include <iomanip>  
03.#include <math.h>  
04.using namespace std;  
05.int main()  
06.{  
07.   double root2 = sqrt( 2.0 );  
08.   int places;  
09. 
10.   cout << setiosflags( ios::fixed)  
11.        << "Square root of 2 with precisions 0-9.\n" 
12.        << "Precision set by the " 
13.        << "precision member function:" << endl;  
14. 
15.   for ( places = 0; places <= 9; places++ ) {  
16.      cout.precision( places );  
17.      cout << root2 << '\n';  
18.   }  
19. 
20.   cout << "\nPrecision set by the " 
21.        << "setprecision manipulator:\n";  
22. 
23.   for ( places = 0; places <= 9; places++ )  
24.      cout << setprecision( places ) << root2 << '\n';  
25. 
26.   return 0;  
27.}

[ZZ]c++ cout 格式化输出浮点数、整数及格式化方法的更多相关文章

  1. [转载] c++ cout 格式化输出浮点数、整数及格方法

    C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢...? 下面的方法是在网上找到的,如果各位有别的办法谢谢留下... iomanip.h是I/O流控制头文件,就像C里面的格式 ...

  2. C++格式化输出浮点数

    主要内容 介绍C++中如何格式化输出浮点数. 控制浮点数输出格式需要包含iomanip头文件. 使用fixed来控制输出的浮点数的小数位是固定的.可参考http://en.cppreference.c ...

  3. //%f表示以十进制格式化输出浮点数 %.2f

    //%f表示以十进制格式化输出浮点数 String s1 ="评分: %.1f"; String s2 = String.format(s1, 8.0); System.out.p ...

  4. [No000063]Python格式化输出

    python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...

  5. Python学习教程(learning Python)--1.2.2 Python格式化输出基础

    本节讨论为何要格式化输出数据? 先看一段代码吧,本程序的功能是计算月支付金额. amount_due = 5000.0 #年支付金额 monthly_payment = amount_due / 12 ...

  6. Python之格式化输出讲解

    1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...

  7. 7.20.01 java格式化输出 printf 例子

    java格式化输出 printf 例子 importjava.util.Date; publicclassPrintf { publicstaticvoidmain(String[] args) { ...

  8. Python基本格式化输出

    什么叫格式化输出? 数据按照某种特殊的要求输出 假如输入一个整数,希望整数按照十六进制,八进制输出,如果输入一个小数,希望小数保留后面2位数然后输出,或者以科学计数法的方式来输出小数.字符串的输出希望 ...

  9. Python print函数用法,print 格式化输出

    原文地址:http://blog.csdn.net/zanfeng/article/details/52164124 使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello ...

随机推荐

  1. linux下shellcode提取常用到的命令

    汇编语言的汇编指令: nasm -f elf xxx.asm    生成xxx.o文件 ld -o xxx  xxx.o  生成可执行文件,不用加参数-s ,否则在提取shellcode的十六进制码的 ...

  2. Java设计模式(2)单态模式(Singleton模式)

    定义:Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 在很多操作中,比如建立目录 数据库连接都需要这样的单线程操作. 还有,singleton能够被状态化 ...

  3. React Native安卓项目打包发布APK步骤

    1.产生签名的key 该过程会用到keytool,开发过安卓的都应该接触过该东西.详细请见密钥和证书管理工具.在项目的主目录(不是android文件夹)中执行: --生成签名key,注意记下你的密钥和 ...

  4. 简单MVVM教程

    WPF+MVVM的本地桌面程序为背景,这样一来我们可以不去操心服务器那部分的事情,更加专注我们的MVVM.我打算把最重要的部分放到开头来讲,而接下来这最重要的部分却是全篇教程唯一没有代码的部分.好,下 ...

  5. 磁盘映射: between 宿主机 and 客户机

    一.虚拟机映射到宿主机     在虚拟机关机的状态下,双击右侧设备栏里硬盘,在弹出的窗口中单击“实用程序“,选择“映射”.打开映射虚拟磁盘的窗口,其中的“卷”就是你希望映射虚拟机中的哪个分区到主机,如 ...

  6. 关于Unity中旧版动画系统的使用

    Unity在5.X以后,有一个旧版的动画系统和新版的动画系统. 新版的动画系统是使用Unity动画编辑器来调的,调动画和控制动画 旧版的动画系统是用其他的第三方软件调好后导出到一个FBX文件里面,就是 ...

  7. 苹果Mac OS系统修改Hosts文件的方法

    使用苹果Mac OS X系统的用户有很多,近期也有不少童鞋问我Mac怎么修改hosts,修改hosts的方式有很多,下面我就整理两种比较方便的方法吧,希望能够帮到大家. 在某些时候可能遇到了需要修改系 ...

  8. android WiFi ASSOC_REJECT 流程跟踪

    Android设备在于AP关联时,如果AP返回关联拒绝帧,Android设别会把AP加入黑名单中. 黑名单中的设备将会在扫描时,延时一段时间放在后面处理. 代码以及log基于SDM450, Andro ...

  9. e866. 确定可用外观

    UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels(); for (int i=0; i<info.len ...

  10. unity-----------------------四元数与欧拉旋转方法

    转:http://blog.csdn.net/treepulse/article/details/49281295 Transfrom.eulerAngles public float yRotati ...