使用setw(x)而不是put_money对齐的带有十进制小数的C ++ cout列表 [英] C++ cout list with decimals aligned using setw(x) not put_money

查看:112
本文介绍了使用setw(x)而不是put_money对齐的带有十进制小数的C ++ cout列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++代码运行良好,但是当前向右输出值,但向左对齐,并且未将小数点对齐。无法使用put_money,我还缺少什么?

C++ Code runs well, but currently outputting values to right but justified left and not lining up on the decimal. Can't use put_money, what am I missing?

尝试使用fprint和put_money,并与同学确认我们应该使用setw(x)。

Attempted using fprint and put_money, confirmed with classmate we're supposed to use setw(x).

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
const double taxRate = 0.09;
const double laborCPH = 35.0; //where CPH is Cost Per Hour
double costParts;
double costLabor;
double totalTax;
double totalDue;
string name;
double laborHours;

     cout << "What is your name? ";
     cin >> name;

     cout << "How many hours of labor needed? ";
     cin >> laborHours;

costLabor = laborHours * laborCPH;

     cout << "What was the cost of the parts and supplies? ";
     cin >> costParts;
     cout << endl << endl;


totalTax = costParts * taxRate;

totalDue = costParts + totalTax + costLabor;

     cout.precision(2);
     cout << setw(25) << left << "Customer Name " << fixed << right << internal << name << endl;
     cout << setw(25) << left << "Hours of Labor " << fixed << right << internal  << laborHours << endl;
     cout << setw(25) << left << "Cost for Labor " << fixed << right << internal  << costLabor << endl;
     cout << setw(25) << left << "Parts and Supplies " << fixed << right << internal  << costParts << endl;
     cout << setw(25) << left << "Tax " << fixed << right << internal  << totalTax << endl;
     cout << setw(25) << left << "Total Amount Due " << fixed << right << internal  << totalDue << endl;

 return 0;
}

实际输出:

What is your name? Jones
How many hours of labor needed? 4.5
What was the cost of the parts and supplies? 97


Customer Name            Jones
Hours of Labor           4.50
Cost for Labor           157.50
Parts and Supplies       97.00
Tax                      8.73
Total Amount Due         263.23

所需的输出:

What is your name? Jones
How many hours of labor needed? 4.5
What was the cost of the parts and supplies? 97


Customer Name            Jones
Hours of Labor             4.50
Cost for Labor           157.50
Parts and Supplies        97.00
Tax                        8.73
Total Amount Due         263.23


推荐答案

使用<$ c $时c> std :: internal 在打印之前填充剩余空间,它将所有剩余空间打印到终端。如果仔细查看输出,可以看到数字开头有25个字符(如果是第一行,则为您的名字。)

When you use std::internal to pad the rest of the space before printing, it will print all of the remaining space to the terminal. If you look closely at your output, you can see there's 25 characters before the start of your numbers (or your name if it's the first line.)

通过计算将要打印的字符串的长度,并从 setw()调用中减去该长度,这对于固定精度的浮点数和双精度数会变得很复杂。幸运的是,有一种更简单的方法!

You can get around this by calculating the lengths of the strings you'll be printing and subtracting that from the setw() call, but that gets complicated for fixed-precision floats and doubles. Luckily, there's an easier way!

如Fred Larson在评论中指出的,解决此问题的一种好方法是添加第二个 setw() std :: internal 在您第一次调用 std :: internal 之后,就像这样:

As Fred Larson pointed out in the comments, a great way to get around this is by adding a second setw() and std::internal after your first call to std::internal like so:

     cout.precision(2);
     cout << setw(20) << left << "Customer Name " << fixed << right << internal << setw(7) << internal << name << endl;
     cout << setw(20) << left << "Hours of Labor " << fixed << right << internal << setw(7) << internal << laborHours << endl;
     cout << setw(20) << left << "Cost for Labor " << fixed << right << internal << setw(7) << internal << costLabor << endl;
     cout << setw(20) << left << "Parts and Supplies " << fixed << right << internal << setw(7) << internal << costParts << endl;
     cout << setw(20) << left << "Tax " << fixed << right << internal << setw(7) << internal << totalTax << endl;
     cout << setw(20) << left << "Total Amount Due " << fixed << right << internal  << setw(7) << internal << totalDue << endl;

这允许终端在输出中填充空格,然后在数字或字符串中附加空格,然后最后打印所有对齐的数字。只要确保您希望第二个 setw()有足够的空间来容纳大量数字即可。

This allows the terminal to fill the output with spaces, then buffer your numbers or strings with additional spaces, and then finally print all your numbers aligned. Just be sure to give your second setw() enough space to accommodate large numbers, if you're expecting them.

在此处尝试!

这篇关于使用setw(x)而不是put_money对齐的带有十进制小数的C ++ cout列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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