如何在cpp中小数点后打印两位数

时间:2015-01-14 08:51:03

标签: c++

如何在cpp中小数点后打印两位数字:

#include<iostream>  

using namespace std; 

int main() 
{ 
    int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 
    cout<<"Enter total number of processes(maximum 20):"; 
    cin>>n; 
    cout<<"\nEnter Process Burst Time\n"; 
    for(i=0;i<n;i++) 
    { 
        cout<<"P["<<i+1<<"]:"; 
        cin>>bt[i]; 
    } 
    wt[0]=0;    //waiting time for first process is 0 
    //calculating waiting time 
    for(i=1;i<n;i++) 
    { 
        wt[i]=0; 
         for(j=0;j<i;j++) 
             wt[i]+=bt[j];
    } 
    cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time"; 
    //calculating turnaround time 
    for(i=0;i<n;i++)
    { 
         tat[i]=bt[i]+wt[i]; 
         avwt+=wt[i]; 
         avtat+=tat[i]; 
         cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i]; 
    } 
    avwt/=i; 
    avtat/=i; 
    cout<<"\n\nAverage Waiting Time:"<<avwt; 
    cout<<"\nAverage Turnaround Time:"<<avtat; 
    return 0; 
 } 

2 个答案:

答案 0 :(得分:2)

使用setprecision

std::cout << std::setprecision(3) << std::fixed ;
std::cout << avwt;

答案 1 :(得分:2)

你可以使用printf

printf("%.2f", avwt);

请注意,您还可以在.

之前指定要打印的位数