我如何对齐美元数额,使美分匹配在c++中

How do I align dollar amounts so cents match up in C++?

本文关键字:c++ 何对齐 对齐 美元      更新时间:2023-10-16

我的程序执行得很好,但是我想知道如何对齐我的输出,使美分排列而不是美元。

我们几个星期前才开始上课,所以我们还没有复习这个。我的教授说现在如果他们不一致也没关系,我想我只是有点强迫症。而且,我觉得它看起来干净多了。

还有,如果账单是38.40美元,那是四位有效数字吗?抱歉,我有段时间没学数学了。在我的输出中,由于某些原因,我得到了多达五个有效数字。我最多有四个。如何使用setprecision解决这个问题?

cout << "Bill t t   $  " << bill << endl;
cout << "Tax at 10.5% t t $"<<tax<< endl;
cout << "Sub-total t t $"<<subTotal<< endl;
cout << "Tip at 20% t t $"<<tip<< endl;
cout << endl;
cout << "Total Bill t t t $"<<totalBill<< endl;

如您所见,我一直在尝试使用制表符转义。正如回复建议的那样,我应该使用setw?

编辑9/10:

除了账单,我把所有的钱都弄成两位小数了,我不知道该怎么修。谢谢你给我提供的所有信息,但对于我们现在正在做的事情来说,它太先进了,所以我只是手动对齐了东西。我仍然需要添加setw,然后在那里修复所有内容。我只是想问为什么账单只有三位数。这可能是一些超级简单的东西,我不知道。

 // Declare variables
double bill, tax, subTotal, tip, totalBill;
// Variables
bill = 38.40;
tax = .105;
tip = .20;
// Calculate the tax
tax = bill * .105;
// Calculate sub-total of bill
subTotal = bill + tax;
// Calculate tip
tip = subTotal * .20;
// Calculate total amount of bill
totalBill = subTotal + tip;
cout << "Bill" "         $ " << setprecision(4) << bill << endl;
cout << "Tax at 10.5%" " $ " << setprecision(3) << tax << endl;
cout << "Sub-total" "    $ " << setprecision(4) << subTotal << endl;
cout << "Tip at 20%" "   $ " << setprecision(3) << tip << endl;
cout << endl;
cout << "Total Bill" "   $ " << setprecision(4) << totalBill << endl;

编辑:我"fixed" it。现在一切都好了

如果你正在印钱,我建议你看看c++的货币I/o。
std::put_money将确保您符合国际标准,并以正确的舍入/精度打印。

为USD设置std::cout的区域设置。
std::showbase决定是否打印$

  //settings for printing as USD
  std::cout.imbue(std::locale("en_US.utf8"));
  std::cout << std::showbase;

使用std::setw和std::left进行格式化。
下面是打印数据的示例:

#include <iostream>
#include <string>
#include <iomanip>  
//row data from example
struct Row{
  std::string description;
  float amount;
};
//function for printing a row
void Print(Row row);
int main(){
  //example rows
  Row a{"Bill",3840};
  Row b{"Tax at 10.5%",403};
  Row c{"Sub-total",4243};
  Row d{"Tip at 20%",848};
  Row e{"Total Bill",5091};
  //settings for printing as USD
  std::cout.imbue(std::locale("en_US.utf8"));
  std::cout << std::showbase;
  //format printing
  Print(a);
  Print(b);
  Print(c);
  Print(d);
  std::cout << 'n';
  Print(e);
}
void Print(Row row){
  static const int COLUMN_WIDTH{14};
  std::cout << std::setw(COLUMN_WIDTH) << std::left << row.description;
  std::cout << " " << std::right << std::put_money(row.amount) << 'n';
}
结果:

Bill           $38.40
Tax at 10.5%   $4.03
Sub-total      $42.43
Tip at 20%     $8.48
Total Bill     $50.91

一种可能的方法是使用setw。

cout<<setw(5)<<4.55<<endl;
cout<<setw(5)<<44.55<<endl;
output:
 4.55
44.55

更新:正如Jonathan Leffler所指出的,<<运算符重置了宽度,因此代码被更新以显示它应该被重复。

我会这样做:

std::cout << std::setw(15) << std::left << "Bill";
std::cout << std::setw(15) << std::right << std::fixed << std::setprecision(2) << bill << std::endl;
std::cout << std::setw(15) << std::left << "Tax @ 10.5%";
std::cout << std::setw(15) << std::right << std::fixed << std::setprecision(2) << tax << std::endl;

这将每个"列"的输出宽度设置为15个字符,这样您就不必依赖制表符了。所有的"标签"将左对齐,所有的价格将右对齐,并打印到小数点后2位。这比依赖制表符更健壮,在制表符中,您无法控制使用多少字符。你不能对制表符进行正确的对齐