将float限制为两位小数

时间:2016-02-27 20:37:34

标签: c++

这就是我目前的作业,我想知道如何将结果限制为只显示两位小数。

const float minWage = 7.50;
const float numTax = .15;
float numHours = 0.0;
float grossPay = 0.0;
float withHoldTax = 0.0;
float netPay = 0.0;

cout << "Enter the total hours worked" << endl;
cin >> numHours;

grossPay = numHours * minWage;
withHoldTax = numTax * grossPay;
netPay = grossPay - withHoldTax;

cout << "You worked " << numHours << "hours." << endl;
cout << " " << endl;
cout << "Gross Pay is $" << grossPay << endl;
cout << "Tax collected is $" << withHoldTax << endl;
cout << "Net pay is $" << netPay << endl;




return 0;
}

1 个答案:

答案 0 :(得分:0)

尝试使用fixed << setprecision(2) <<