c++中控制输出精度的几种方式

//控制输出精度;可以用iostream中的precision或是用iomanip中的setrecision;
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    float a=4.5891589;
	//cout<<fixed;
	//cout<<setprecision(3)<<a<<endl;

    cout.precision(2);//默认模式下,它指的是显示的总位数;
    cout<<a<<endl;
    
    cout.setf(ios_base::fixed);//定点计数法与科学计数法的精度指小数点位数;
    
    cout<<a<<endl;
	
    return 0; 
} 

 

posted @ 2012-07-27 09:42  zh yu  阅读(1568)  评论(0编辑  收藏  举报