在何处以及如何将C++代码格式化为小数点后2位

Where and How to format a C++ code to 2 decimal places

本文关键字:格式化 小数点 2位 代码 C++ 在何处      更新时间:2023-10-16

◾使用输出语句显示输入数据和计算结果;显示格式化为两位小数的十进制值 这就是我遇到困难的地方这个特定项目已经完成了所有其他工作,所以例如,当你使用4.4444作为工资率,使用10作为小时数时,它应该打印出常规工资是44.44!不是44.444!

{
// declare variables
int Id = 0;
double hours = 0.00;
double payrate = 0.00;
char ucode = 'A';
double rpay = 0.00;
double opay = 0.00;
double gpay = 0.00;
double ftax = 0.00;
double npay = 0.00;
cout <<"nWelcome Please Enter Employee Info";
//Input Data from keyboard
cout << "nEnter Employee's ID ";
cin >> Id;
cout << "nEnter Employee's Hours worked ";
cin >> hours;
cout << "nEnter Employee's Pay Rate $";
cin >> payrate;
cout << "nEnter Employee's union code ";
cin >> ucode;
cout << "nEmployee's Id " << Id << "nEmployee's Hours " << hours << "nEmployee's Payrate " << payrate << "nEmployee's Union Code " << ucode;
//Calculate regular pay and/or overtime pay. 
if ( hours <= 40 ){
  rpay = hours * payrate;
  gpay = rpay;
  cout<< "nGross pay is $" << gpay;}
if ( hours > 40 ){
   rpay = 40 * payrate;
   opay = payrate * (1.5 *(hours-40));
   gpay = rpay + opay;
   cout<< "nGross pay is $" << gpay;}
//Calculate the Gross Pay.   
if ( gpay <= 1000 ) {
   ftax = .10 * gpay;
   cout << "nThe Federal tax due is $" << ftax;
   }
 else if ( gpay > 1000 && gpay <= 2000){
   ftax = .15 * gpay;
   cout << "nThe Federal tax due is $" << ftax; 
     }
  else if ( gpay > 2000){
   ftax = .25 * gpay;
   cout << "nThe Federal tax due is $" << ftax;
     }
//Calulating Netpay.     
switch ( ucode )
{
       case 'A' : cout << "nEmployee owes $25 to Union ";
                  npay = (gpay - (ftax + 25));
                  cout << "nEmployee's Net Pay is $" << npay;
                  break;
       case 'a' : cout << "nEmployee owes $25 to Union ";
                  npay = (gpay - (ftax + 25));
                  cout << "nEmployee's Net Pay is $" << npay;
                  break;
       case 'B' : cout << "nEmployee owes $50 to Union ";
                  npay = (gpay - (ftax + 50));
                  cout << "nEmployee's Net Pay is $" << npay;
                  break;
       case 'b' : cout << "nEmployee owes $50 to Union ";
                  npay = (gpay - (ftax + 50));
                  cout << "nEmployee's Net Pay is $" << npay;
                  break;
       case 'C' : cout << "nEmployee owes $75 to Union ";
                  npay = (gpay - (ftax + 75));
                  cout << "nEmployee's Net Pay is $" << npay;
                  break;
       case 'c' : cout << "nEmployee owes $75 to Union ";
                  npay = (gpay - (ftax + 75));
                  cout << "nEmployee's Net Pay is $" << npay;
                  break;          
       default:
                  cout << "nThere is no such Union Code ";
                  npay = (gpay - (ftax + 0));
                  cout << "nEmployee's Net Pay is $ " << npay;
                  }
       cout << "nThank You for using our software, Please Enjoy the rest of your Day! n ";

std::setprecision和std::fixed应该可以做到这一点。

听起来你需要这样的东西:

 std::cout << std::fixed << std::setprecision(2);