10 進数の値を揃えるのに苦労しています。正しい配置と 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;
これは次のようになります。
これは私も好きです: