我很难对齐十进制值。我很确定它是正确对齐和setprecision/fixed的组合,但它似乎没有工作。我知道关于这个主题的其他问题已经被提出,但是我还没有找到明确的解决方案来获得一堆列(独特的cout语句来对齐)。C++ 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;
这是什么样子:
这是我想它太像:
“Totla收集” – 2014-09-19 15:39:32