我如何对齐美元数额,使美分匹配在c++中
How do I align dollar amounts so cents match up in C++?
我的程序执行得很好,但是我想知道如何对齐我的输出,使美分排列而不是美元。
我们几个星期前才开始上课,所以我们还没有复习这个。我的教授说现在如果他们不一致也没关系,我想我只是有点强迫症。而且,我觉得它看起来干净多了。
还有,如果账单是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位。这比依赖制表符更健壮,在制表符中,您无法控制使用多少字符。你不能对制表符进行正确的对齐
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- 64位机器上的C++内存对齐
- 为什么我可以将变量存储在不是其最小对齐方式的倍数的地址?
- 使 std::vector 分配对齐内存的现代方法
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 在 64 位边界上对齐C++结构数组?
- 使用 g++7 构建的代码在访问未对齐的内存时崩溃
- 在 capnp FlatArrayMessageReader 的对齐内存缓冲区中接收 zmq 消息
- 是否值得对齐变量?
- 初始化派生结构的基部分/意外打包派生结构字段以对齐基结构的间隙
- 对齐和对齐的实际用例C++关键字
- 如何减少代码的运行时间以对齐文本?
- 指向包含对齐 C 结构C++类的 C 指针的对齐问题
- Linux C++ 中的页面对齐内存分配
- C++ 类层次结构中的"对齐"是什么意思?
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 具有调整对齐方式的类型定义
- 对齐方式与指针中尾随零的数量有何关系