我很难对齐十进制值。我很确定它是正确对齐和 setprecision/fixed 的组合,但它似乎不起作用。我知道已经就该主题提出了其他问题,但我还没有找到一个明确的解决方案来获得一堆列(要对齐的唯一 cout 语句)。
这是我的一段代码:
double total_collect, sales, country_tax, state_tax, total_tax;
const double STATE_TAX_RATE = 0.04, COUNTRY_TAX_RATE = 0.02;
// Compute taxes
total_collect = 100;
sales = 100 / 1.06 ;
country_tax = sales * COUNTRY_TAX_RATE;
state_tax = sales * STATE_TAX_RATE;
total_tax = country_tax + state_tax;
//Display
cout << setiosflags(std::ios::right) ;
cout << "Totla Collected: " << setw(7) << "$ " << fixed << setprecision(2) << right << total_collect << endl;
cout << "Sales: " << setw(17) << "$ " << fixed << setprecision(2) << right << sales << endl;
cout << "Country Sales Tax: " << setw(5) << "$ " << fixed << setprecision(2) << right << country_tax << endl;
cout << "State Sales Tax: " << setw(7) << "$ " << fixed << setprecision(2) << right << state_tax << endl;
cout << "Total Sales Tax: " << setw(7) << "$ " << fixed << setprecision(2) << left << total_tax << endl << endl;
这是它的样子:
这也是我想要的: