【问题标题】:C++ cout list with decimals aligned using setw(x) not put_money使用 setw(x) 而非 put_money 对齐小数的 C++ cout 列表
【发布时间】:2020-01-09 16:33:46
【问题描述】:

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

【问题讨论】:

  • 尝试为第二列值添加宽度,例如setw(7)
  • 谢谢,@FredLarson

标签: c++ decimal cout setw


【解决方案1】:

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

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

正如 Fred Larson 在 cmets 中指出的那样,解决此问题的一个好方法是在您第一次调用 std::internal 之后添加第二个 setw()std::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!

【讨论】:

  • @AndrewMedina 很高兴为您提供帮助,感谢 Fred Larson 的建议。如果它解决了您的问题,请记住接受答案。欢迎使用 StackOverflow!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
相关资源
最近更新 更多