gpt4 book ai didi

C++ cout 小数对齐

转载 作者:行者123 更新时间:2023-12-02 05:04:21 26 4
gpt4 key购买 nike

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

最佳答案

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

理想的解决方案是使用 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.00Sales:              $9,433.96Country Sales Tax:    $188.68State Sales Tax:      $377.36Total Sales Tax:      $566.04

关于C++ cout 小数对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25936009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com