使用setw(x)而不是put_money对齐的C ++ cout列表(带小数)

时间:2019-09-06 20:14:30

标签: c++ decimal cout setw

C ++代码运行良好,但是当前将值向右输出,但向左对齐,并且在小数点后对齐。无法使用put_money,我想念什么?

尝试使用fprint和put_money,并与同学确认我们应该使用setw(x)。

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
const double taxRate = 0.09;
const double laborCPH = 35.0; //where CPH is Cost Per Hour
double costParts;
double costLabor;
double totalTax;
double totalDue;
string name;
double laborHours;

     cout << "What is your name? ";
     cin >> name;

     cout << "How many hours of labor needed? ";
     cin >> laborHours;

costLabor = laborHours * laborCPH;

     cout << "What was the cost of the parts and supplies? ";
     cin >> costParts;
     cout << endl << endl;


totalTax = costParts * taxRate;

totalDue = costParts + totalTax + costLabor;

     cout.precision(2);
     cout << setw(25) << left << "Customer Name " << fixed << right << internal << name << endl;
     cout << setw(25) << left << "Hours of Labor " << fixed << right << internal  << laborHours << endl;
     cout << setw(25) << left << "Cost for Labor " << fixed << right << internal  << costLabor << endl;
     cout << setw(25) << left << "Parts and Supplies " << fixed << right << internal  << costParts << endl;
     cout << setw(25) << left << "Tax " << fixed << right << internal  << totalTax << endl;
     cout << setw(25) << left << "Total Amount Due " << fixed << right << internal  << totalDue << endl;

 return 0;
}

实际输出:

What is your name? Jones
How many hours of labor needed? 4.5
What was the cost of the parts and supplies? 97


Customer Name            Jones
Hours of Labor           4.50
Cost for Labor           157.50
Parts and Supplies       97.00
Tax                      8.73
Total Amount Due         263.23

所需的输出:

What is your name? Jones
How many hours of labor needed? 4.5
What was the cost of the parts and supplies? 97


Customer Name            Jones
Hours of Labor             4.50
Cost for Labor           157.50
Parts and Supplies        97.00
Tax                        8.73
Total Amount Due         263.23

1 个答案:

答案 0 :(得分:0)

在打印之前使用std::internal填充剩余空间时,它将剩余空间的 all 全部打印到终端。如果仔细查看输出,可以看到数字开头(如果是第一行,则为25个字符)

您可以通过计算将要打印的字符串的长度,并从setw()调用中减去该字符串的长度来解决此问题,但是对于固定精度的浮点数和双精度数,这变得很复杂。幸运的是,有一种更简单的方法!

如Fred Larson在评论中所指出的,解决此问题的一种好方法是在您第一次致电setw()之后添加第二个std::internalstd::internal,如下所示:

     cout.precision(2);
     cout << setw(20) << left << "Customer Name " << fixed << right << internal << setw(7) << internal << name << endl;
     cout << setw(20) << left << "Hours of Labor " << fixed << right << internal << setw(7) << internal << laborHours << endl;
     cout << setw(20) << left << "Cost for Labor " << fixed << right << internal << setw(7) << internal << costLabor << endl;
     cout << setw(20) << left << "Parts and Supplies " << fixed << right << internal << setw(7) << internal << costParts << endl;
     cout << setw(20) << left << "Tax " << fixed << right << internal << setw(7) << internal << totalTax << endl;
     cout << setw(20) << left << "Total Amount Due " << fixed << right << internal  << setw(7) << internal << totalDue << endl;

这使终端可以用空格填充输出,然后用其他空格缓冲数字或字符串,最后打印所有对齐的数字。只需确保第二个setw()有足够的空间来容纳大量数字即可。

Try it here!

相关问题