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()<<d<<endl; //精度为3,输出3位数
cout<<setiosflags(ios::fixed)<<d<<endl;//精度为3,定点输出,输出3位小数
cout<<setiosflags(ios::fixed)<<setprecision()<<d<<endl;//位数不够,末尾添0 输出结果:
11.2346
11.2
11.23456
11.2345600 C++格式化输出浮点数 view plaincopy to clipboardprint?
.#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 ;
.}
#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 ;
} 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?
.#include <iostream.h>
.
.main(void)
.{
. float a=100100.0, b=0.08;
. cout.setf(ios::right|ios::scientific|ios::showpoint);
. cout.width();
. cout <<(-a*b);
.
. return ;
.}
#include <iostream.h> main(void)
{
float a=100100.0, b=0.08;
cout.setf(ios::right|ios::scientific|ios::showpoint);
cout.width();
cout <<(-a*b);
return ;
} -8.008000e+003 view plaincopy to clipboardprint?
.#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 = . ; radius <= 3.0 ; radius += .)
. cout << "radius = "
. << setprecision(numeric_limits<double>::digits10 + )
. << std::scientific << radius<< " area = "
. << std::setw() << setprecision()<< std::fixed << pi * radius * radi
.us << endl;
. return ;
.}
#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 = . ; radius <= 3.0 ; radius += .)
cout << "radius = "
<< setprecision(numeric_limits<double>::digits10 + )
<< std::scientific << radius<< " area = "
<< std::setw() << setprecision()<< std::fixed << pi * radius * radi
us << endl;
return ;
} 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?
.#include <iostream>
.#include <iomanip>
.#include <string>
.
.using namespace std;
.
.int main( ) {
.
. ios_base::fmtflags flags = cout.flags( );
.
. double pi = 3.14285714;
.
. cout << "pi = " << setprecision() << pi << '\n';
.
. cout.flags(flags);
.}
#include <iostream>
#include <iomanip>
#include <string> using namespace std; int main( ) { ios_base::fmtflags flags = cout.flags( ); double pi = 3.14285714; cout << "pi = " << setprecision() << pi << '\n'; cout.flags(flags);
} pi = 3.1429 view plaincopy to clipboardprint?
.#include <iostream>
.#include <iomanip>
.#include <math.h>
.using namespace std;
.int main()
.{
. double root2 = sqrt( 2.0 );
. int places;
.
. cout << setiosflags( ios::fixed)
. << "Square root of 2 with precisions 0-9.\n"
. << "Precision set by the "
. << "precision member function:" << endl;
.
. for ( places = ; places <= ; places++ ) {
. cout.precision( places );
. cout << root2 << '\n';
. }
.
. cout << "\nPrecision set by the "
. << "setprecision manipulator:\n";
.
. for ( places = ; places <= ; places++ )
. cout << setprecision( places ) << root2 << '\n';
.
. return ;
.}

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

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

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

  2. C++ cout格式化输出(转)

    C++ cout格式化输出(转) 这篇文章主要讲解如何在C++中使用cout进行高级的格式化输出操作,包括数字的各种计数法(精度)输出,左或右对齐,大小写等等.通过本文,您可以完全脱离scanf/pr ...

  3. C++ cout格式化输出完全攻略

    写算法题的时候突然发现自己忘记基本的C++:cout格式化输出了,赶紧拉出以前的C++学习笔记重新看一看. 部分内容来自教程:C语言中文网(一个很棒的网站) 有时希望按照一定的格式进行输出,如按十六进 ...

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

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

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

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

  6. C++ cout 格式化输出方法

    C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢...? iomanip是I/O流控制头文件,就像printf的格式化输出一样. 以下是一些常用的: dec 置基数为10 相 ...

  7. C++ cout格式化输出

    表1:C++ 流操纵算子 流操纵算子 作  用 *dec 以十进制形式输出整数 常用 hex 以十六进制形式输出整数 oct 以八进制形式输出整数 fixed 以普通小数形式输出浮点数 scienti ...

  8. C++ cout格式化输出(输出格式)完全攻略

    使用流操作算子 它们都是在头文件 iomanip 中定义的:要使用这些流操纵算子,必须包含该头文件. 表1:C++ 流操纵算子 流操纵算子 作  用 *dec 以十进制形式输出整数 hex 以十六进制 ...

  9. cout 格式化输出

    一直习惯于C语言的printf函数来打印,突然有一天要用cout来打印,发现有点不适应. 原来cout也是有格式化输出的. 首先要引入头文件 #include<iostream> // 在 ...

随机推荐

  1. 更改SharePoint 2010 顶部导航为下拉菜单样式

      更改SharePoint 2010 顶部导航为下拉菜单样式 最后的效果图: 假如一个网站集顶级站点下面有子网站:sub site1,该子站点下面又有两个子站点:sub site1_1,sub si ...

  2. poj1166

    爆搜就可以过,不过我用了迭代加深. 注意每个操作最多进行4次 #include <cstdio> #include <cstdlib> using namespace std; ...

  3. ShortestPath:Silver Cow Party(POJ 3268)

    牛的聚会 题目大意:一群牛在一块农田的不同的点,现在他们都要去到同一个地方开会,然后现在从那个地方回到原来的位置,点与点之间的连线都是单向的,并且通过一个路径需要一定时间,问你现在哪只牛需要最多的时间 ...

  4. object-c学习笔记

    原文地址 最近开始学习object-c,分享一下学习oc的经验以及对oc的理解,其中难免会有错误,请大家理解. 对初学者来说,objective-c存在了很多令人费解的写法,当然也包括我! 我刚开始看 ...

  5. Session入门

    Session是运行在服务器的,不可造假,例如:医生需要一个私人账本,记录病人编号和身份的对应关系.由于身份证无法造假,所以能够保证信息不被假冒.两点:身份证无法造假,这个身份证就可以唯一标识这个用户 ...

  6. (六)STM32的时钟系统

    在STM32中,一共有5个时钟源,分别是HSI.HSE.LSI.LSE.PLL (1) HSI是高速内部时钟,RC振荡器,频率为8MHz: (2) HSE是高速外部时钟,可接石英/陶瓷谐振器,或者接外 ...

  7. php 增删改查练习

    添加界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  8. 10.组合模式(Composite Pattern)

    using System; using System.Collections.Generic; namespace ConsoleApplication8 { class Program { stat ...

  9. 让Entity Framework支持MySql数据库(转载)

    转载地址:http://www.cnblogs.com/wintersun/archive/2010/12/12/1903861.html Entity Framework 4.0 也可以支持大名鼎鼎 ...

  10. Java Hour 14 多线程基础

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. 多线程 这个是基 ...