C ++ cout十进制对齐

时间:2014-09-19 14:13:31

标签: c++ alignment decimal fixed iomanip

我很难对齐十进制值。我很确定它是右对齐和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; 

这就是它的样子:
enter image description here

这就是我想要的样子:
enter image description here

1 个答案:

答案 0 :(得分:1)

您正在&#34; $&#34;上设置宽度,这样可以很好地对齐它们。但您还需要为值本身设置它。我在每个setw(8)之前添加了fixed并且很好地对齐了它们,除了最后一个有left而不是right的情况。您可能需要不同的宽度值,但每行应该相同。

理想的解决方案是使用std::put_money(我在评论中看到你不能这样做,但也许这会帮助其他人阅读这个答案)。我已经增加了美元金额来说明千位分隔符并修复了一两个错误:

#include <locale>
#include <iostream>
#include <iomanip>

int main()
{
    double total_collect, sales, country_tax, state_tax, total_tax;
    const double STATE_TAX_RATE = 0.04, COUNTRY_TAX_RATE = 0.02;
    const auto TAX_WIDTH = 10;
    const auto LABEL_WIDTH = 19;

    // Compute taxes
    total_collect = 10000;
    sales = total_collect / 1.06 ;
    country_tax = sales * COUNTRY_TAX_RATE;
    state_tax = sales * STATE_TAX_RATE;
    total_tax = country_tax + state_tax;

    //Display
    std::cout.imbue(std::locale("en_US.utf8"));
    std::cout << std::setw(LABEL_WIDTH) << std::left << "Total Collected: "
        << std::setw(TAX_WIDTH) << std::right << std::showbase
        << std::put_money(total_collect * 100.0) << std::endl;
    std::cout << std::setw(LABEL_WIDTH) << std::left << "Sales: "
        << std::setw(TAX_WIDTH) << std::right << std::showbase
        << std::put_money(sales * 100.0) << std::endl;
    std::cout << std::setw(LABEL_WIDTH) << std::left << "Country Sales Tax: "
        << std::setw(TAX_WIDTH) << std::right << std::showbase
        << std::put_money(country_tax * 100.0) << std::endl;
    std::cout << std::setw(LABEL_WIDTH) << std::left << "State Sales Tax: "
        << std::setw(TAX_WIDTH) << std::right << std::showbase
        << std::put_money(state_tax * 100.0) << std::endl;
    std::cout << std::setw(LABEL_WIDTH) << std::left << "Total Sales Tax: "
        << std::setw(TAX_WIDTH) << std::right << std::showbase
        << std::put_money(total_tax * 100.0) << std::endl << std::endl;
}

我得到了这个输出:

Total Collected:   $10,000.00
Sales:              $9,433.96
Country Sales Tax:    $188.68
State Sales Tax:      $377.36
Total Sales Tax:      $566.04