cout 格式化输出

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.} 


将 cout 的 flag 保存到变量, 以便修改后的恢复

  
  
ostream::fmtflags old = cout.flag() ; // 无参将返回当前 flag 值
cout.flag(old) ; // 恢复到原先保存的值


将 bool 值以 literals 输出

  
  
cout << " numeric : " << true << " or " << false << endl ; // 1 or 0
cout << " literals : " << boolalpha << true << " or " << false << endl ; // true or false
cout << " literals : " << boolalpha << 0 << endl ; // 0 原因: 0 在cout中不等价于 false

一旦我们使用 boolalpha 将改变 cout 对 bool 值的输出格式. 此后的 cout 都会将 bool 输出为 literals.


将 bool 值以 numeric 输出

  
  
cout << " numeric : " << noboolalpha << true << " or " << false << endl ; // 1 or 0

从此以后, cout 对 bool 值的输出将恢复 numeric 格式


指定 Integral Values 的 Base

  
  
const int ival = 17 ; // 'ival' is constant, so value never change
cout << " oct : " << oct << ival << endl ; // 21 : 8 进制
cout << " dec : " << dec << ival << endl ; // 17 : 10 进制
cout << " hex : " << hex << ival << endl ; // 11 : 16 进制
cout << " hex : " << hex << 17.01 << endl ; // 17.01 : 不受影响
如 boolalpha 一样, oct, dec, hex 也是 persistent. 一旦改变, 将影响后续的输出格式.


显示表明 Integer Values 的 Base

  
  
cout << showbase ; // Show base when printing integral values
cout << " oct : " << oct << ival << endl ; // 21 : 8 进制
cout << " dec : " << dec << ival << endl ; // 017 : 10 进制
cout << " hex : " << hex << ival << endl ; // 0x11 : 16 进制
cout << " hex : " << hex << 17.01 << endl ; // 17.01 : 不受影响
cout << noshowbase ; // Reset state of the stream

若想改变16进制字母的大小, 可以结合 uppercase/nouppercase

  
  
cout << showbase << uppercase ;
cout
<< " hex : " << hex << 15 << endl ; // 0XF 大写形式
cout << nouppercase ;
cout
<< " hex : " << hex << 15 << endl ; // 0xf 小写形式

showbase 与 noshowbase 的作用周期也是 persistent


对于 float/double 型, 有三种格式化控制

一:  输出精度 precision : by default is 6 pricision
   控制了至多一共会输出多少个数字. 
   当要输出的数字多余指定的值时, 将发生 四舍五入(rounded); 
   当要输出的数字少于指定的值时, 则实际输出的数字个数将少于指定值.
   
   
// cout.pricision(4) ; // 等价于 cout <<setprecision(4) ;
cout << setprecision( 4 ) << 12.345678 << endl ; // 12.35 rounded!
cout << setprecision( 10 ) << 12.345678 << endl ; // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
cout << cout.precision() << endl ; // 输出当前精度
 二:  表现形式 notation :  'very large and very small values are printed using scientific notation. other values use fixeddecimal.'
   notation 控制了输出的形式 : 科学计数法(scientific) 和 定点小数(fixed)
   
   
float f = 101 / 6.0 ;
cout
<< fixed << f << endl ; // 16.83334 : 小数点后共6位
cout << scientific << f << endl ; // 1.683333e+001 : 小数点后共6位
  恢复到初始状态
   
   
cout.unsetf(ostream::floatfield) ; // Retrieve to default handling for notation
cout << f << endl ; // 16.8333 : 所有数字共6位
 三:  输出十进制浮点 'By default, when the fractional part of a floating-point value is 0, the decimal point is not displayed. The showpoint manipulator forces the decimal point ot be printed.'
   
   
cout << 10.0 << endl ; // 10
cout << showpoint << 10.0 << endl ; // 10.0000
cout << noshowpoint << endl ; // Revert to default handling of decimal  


输出填充 
Padding the Output

   setw to specify the minimum space for the next numeric or string value.
   
   
cout << setw( 10 ) << 12.3 << endl ; // ______12.3
cout << setw( 10 ) << 12 << 3 << endl ; // ________123

cout
<< setw( 3 ) << 12.345 << endl ; // If the total output is more than 3, it can be extended
 
   left to left-justify the output.
   
   
cout << left ; // left-justify
cout << setw( 5 ) << 12 << setw( 5 ) << 34 << endl ; // 12___34___
 
   right to right-justify the output. Output is right-justified bu default.
   
   
cout << right ; // By default
cout << setw( 5 ) << 12 << setw( 5 ) << 34 << endl ; // 12___34___
 
   internal controls placement of the sign on negative value. internal left-justifies the sign and right-justifies the value, padding any intervening space with blanks.(if setfill not set) 
   
   
cout << internal ; // By default
cout << setw( 5 ) <<- 12 << endl ; // 12___34___
 
    setfill lets us specify an alternative character to use when padding the output. By default, the value is a space.
   
   
cout << setfill( ' * ' ) ; // By default
cout << setw( 5 ) << 12 << endl ; // 12___34___
 

Header Files

   Manipulators Defined in < iomanip>
   
   
setfill( char ch) Fill whitespace with ' ch '
setprecision(
int n) Set floating - point precision to ' n '
setw(
int w) Read or write value to ' w ' characters
setbase(
int b) Output integers in base ' b ' (only ' b ' is 8 / 10 / 16 could the function work)
 
  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值