【问题标题】:C++ cout decimal alignmentC++ cout 十进制对齐
【发布时间】:2014-09-19 14:13:30
【问题描述】:

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

这就是它的样子:

这也是我想要的:

【问题讨论】:

    标签: c++ alignment decimal fixed iomanip


    【解决方案1】:

    您正在设置“$”的宽度,这可以很好地对齐它们。但是您还需要为值本身设置它。我在每个fixed 之前添加了一个setw(8),并且它们很好地对齐,除了最后一个有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;
    }
    

    我得到这个输出:

    总收集:10,000.00 美元 销售额:9,433.96 美元 国家销售税:188.68 美元 州销售税:377.36 美元 总销售税:566.04 美元

    【讨论】:

    • 我试过你的建议,它奏效了。但我不确定发生了什么。我真的希望 $$ 和美元金额之间没有空格。使用您的解决方案,它运行良好,除非我将美元符号移到变量之前,然后一切都搞砸了。
    • @YelizavetaYR:我认为你应该试试std::put_money。我正在研究一个例子。
    • 谢谢 - 是的,我知道这一点,但被告知要在不使用 put_money 的情况下解决这个难题。难题是如何对齐小数,使用什么组合使字符串/和变量与小数对齐。
    • 诀窍是在对齐之前,您必须在字符串化的美元金额上获得“$”。为此,我认为您需要使用std::stringstream
    • 谢谢 - 所以没有干净的方法可以说对齐这些变量(用小数右对齐),然后将其前面的字符串与变量本身对齐?
    猜你喜欢
    • 2010-10-03
    • 1970-01-01
    • 2016-08-23
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多