C ++ cout十进制对齐 [英] C++ cout decimal alignment

查看:133
本文介绍了C ++ cout十进制对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难对齐十进制值.我很确定它是正确对齐和setprecision/fixed的组合,但似乎不起作用.我知道有关该主题的其他问题已经提出,但我还没有找到一种解决方案来获取一堆列(要对齐的唯一cout语句).

I'm having a hard time aligning decimal values. I am pretty sure its a combination of right alignment and setprecision/fixed but it doesn't seem to be working. I know other questions have been asked on the topic but I haven't found a clear solution to getting a bunch of columns (unique cout statements to align).

这是我的代码的一部分:

This is a chunk of my code:

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; 

这是它的样子:

This is what it looks like:

这也是我想要的:

This is what I would like it too like:

推荐答案

您要在"$"上设置宽度,以使它们很好地对齐.但是您还需要为值本身设置它.我在每个fixed之前添加了一个setw(8),并且它们对齐得很好,除了最后一个具有left而不是right的.您可能想要一个不同的宽度值,但是每行应该相同.

You're setting the width on the "$", which aligns them nicely. But you also need to set it for the values themselves. I added a setw(8) before each fixed and that aligned them nicely, except for the last one which has a left instead of a right. You might want a different width value, but it should be the same for each line.

理想的解决方案是使用 std::put_money (I从您的评论中看到您无法做到,但这也许会帮助其他人阅读此答案).我增加了美元金额来说明数千个分隔符,并修复了一个或两个错误:

The ideal solution would be to use std::put_money (I see by your comment that you can't, but perhaps this will help someone else reading this answer). I've increased the dollar amounts to illustrate the thousands separator and fixed a bug or two:

#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

这篇关于C ++ cout十进制对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆