在何处以及如何将C++代码格式化为小数点后2位
Where and How to format a C++ code to 2 decimal places
◾使用输出语句显示输入数据和计算结果;显示格式化为两位小数的十进制值 这就是我遇到困难的地方这个特定项目已经完成了所有其他工作,所以例如,当你使用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);
相关文章:
- C++格式化输出问题
- 用c++把小数点转换成八进制
- 格式化浮点值:返回默认值
- 自动格式化程序> >更改为>>
- 如何从C++中的格式化字符串派生整数?
- 将从格式化文本文件读取的文本数据存储到链表
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 在不使用系统的情况下从C++应用程序格式化 Linux 中的 SD 卡
- 高精度双精度的 Sprintf 格式化问题
- 使用 cin 时接受小数点后的 2 位数字
- 如何在 c++ 中格式化和访问 2D 矢量数组中的数据
- 叮当格式化程序多行格式配置错误
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 从函数(不抛出函数)返回异常以进行消息格式化?
- 从格式化输入操作中获取读取字符数
- 如何从标准输入中检测格式化输入错误?
- 无法从 spdlog 中格式化参数
- 格式化时间 c++ dd/mm/yyyy hh:ss.
- 如何正确格式化csv文件?
- 如何正确格式化c++浮点数到小数点后两位