- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在为我的代码生成此输出时遇到了一些问题。
Apples
10 @ 0.98/UNIT: $9.80
Bananas
1 @ 1.29/UNIT: $1.29
Flank Steak
1 @ 8.82/UNIT: $8.82
Chocolate Ice Cream
1 @ 3.23/UNIT: $3.23
Gym Bag
1 @ 23.12/UNIT: $23.12
ORDER TOTAL:************************************$46.26
我的问题是带有美元符号的总数的小数位对齐。我应该能够使用原始 setw() 代码和右对齐、左对齐来完成它,但我不太确定如何在 $ 和实际数值之间没有空格的情况下实现它。
这是我到目前为止得到的..
void printReceipt(const int cart[], const string productName[], const double prices[], int productCount){
double orderTotal = 0;
for (int i = 0; i < productCount; i++){ //Loop for output of receipt
if (cart[i] != 0){ //Will not output for item not ordered.
cout << productName[i] << endl;
cout << fixed << setprecision(2)
<< setw(3) << left << cart[i]
<< setw(3) << " @ " //Formatting for receipt print
<< setw(6) << prices[i]
<< setw(35) << left << "/UNIT:" << "$"
<< setw(6)<< right << (cart[i] * prices[i]) << endl;
orderTotal = orderTotal + (cart[i] * prices[i]);
}}
cout << fixed << setfill('*') << setw(47)<< left << "ORDER TOTAL:";
cout << setfill(' ') << "$" << setw(6) << right << setprecision(2) << orderTotal;
}
当前输出如下
Apples
5 @ 0.98/UNIT: $ 4.90
Bananas
5 @ 1.29/UNIT: $ 6.45
Flank Steak
5 @ 8.82/UNIT: $ 44.10
Chocolate Ice Cream
5 @ 3.23/UNIT: $ 16.15
Gym Bag
5 @ 23.12/UNIT: $115.60
ORDER TOTAL:***********************************$187.20
最佳答案
您必须分两步执行此操作。
将美元金额 (cart[i]*prices[i]
) 格式化为 std::ostringstream
,仅使用 setprecision
操纵器。没有最小 setw
,所以您得到的只是格式化的数量。使用 str()
从 std::ostringstream
获取字符串表示形式。
通过使用从 str()
返回的字符串的长度,您可以计算定位“$”所需的填充量。
您将计算此字段的填充,而不是固定的 setw(35)
。作为粗略估计,这可能是:
<< setw(42-amount.length()) << left << "/UNIT:" << "$" << amount << std::endl;
其中amount
是你在第一步中得到的格式化后的std::string
。这样的话,这里的宽度会自动调整,给适当的空间金额
。
这本身不会调整行首的单位计数,这也是可变长度。但是在正确处理了 amount
之后,在这里,您应该能够弄清楚如何以相同的方式解决这个问题。
关于c++ - 多个输入和美元符号的 iomanip 格式 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39553537/
我刚刚遇到了将自定义输出运算符与 io-manipulators 相结合的问题。也许我的期望完全不对,但如果 std::cout #include struct foo { int a,b; };
我正在尝试对齐我的输出,但由于某种原因我无法做到我多么想要它,这真的很令人沮丧。标题不会正确对齐。我不知道我是否正确使用了 setw()。 #include using std::cout; usi
我正在尝试获取一个 double 并通过使用 iomanip 并编写我自己的流操纵器将其输出为货币。为此,我编写了一个方法,该方法返回一个 ostream 并将一个 ostream 作为参数(以及两个
为什么 #include #include int main() { std::cout > overload page individually. 预期: But that's just
变量: static const float s_period[] = { 100, 50, 25, 12, 5, 7, 3, 2, 1 }; static const unsigned
我想使用 iomanip 打印以筛选小数点后最多 4 位的数字。 我了解到在默认模式下 setprecision 不仅计算小数点后的数字,还计算整数部分的数字。这段代码 #include #incl
我有以下代码 cout 的不一致行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938047/
在 C++ 程序中,我有一些代码可以打印一个名为 fraction 的类的对象。它的变量是 n(分子)、d(分母)和 sinal(信号:当分数为正时为真,否则为假)。 ostream &operato
一些标准的 iomanip 函数接受一个参数。 我想知道这是如何实现的,例如,我可以用一个函数做类似的事情吗?这确实是我需要的解决方案 this answer ,但我不知道该怎么做。 当我在 http
今天,我了解了setfill()和#include中的其他方法,因此我有使用#include的练习。 我想在控制台上打印它: +-------------------------+ |
在排序和显示大数字时,您通常通常会以符号形式显示大数字。我试图通过使用cpp中的库显示整个数字,但是对于很大的数字却失败了。 //big sorting #include #incl
对于下面的代码: cout << left << setw(20) << "Example Header\n"; cout << setw(60) << setfill('-') << "-" <<
当我注意到一些我不理解的东西时,我才刚刚开始编写程序。我定义了一个名为“时间”的结构。当我尝试定义一个名为“time1”的结构变量时,它发出一个错误:“expected an ';'”。但是当我删除头
这个问题在这里已经有了答案: Setting width in C++ output stream (3 个答案) 关闭 9 年前。 我知道如何设置字段宽度,但只适用于流中的第一个元素。例如。 co
我正在努力学习使用命名空间声明,而不是仅仅说“使用命名空间标准”。我正在尝试将我的数据格式化为小数点后两位,并将格式设置为固定而不科学。这是我的主文件: #include #include #in
我用 C++ 创建了一个 Vector 类,它非常适合我的问题。我现在正在清理它,我遇到了以下代码: std::ostream& operator<<(std::ostream &output, co
完成一个长项目,最后一步是确保我的数据排列在正确的列中。简单的。只有我在这方面遇到了麻烦,而且我在这方面的时间比我承认观看了很多视频的时间还要长,而且我真的不知道该怎么做所以这是我遇到问题的一小段代码
我正在尝试使用 iomanip 格式化我的代码输出: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2) << "M
我是 C++ STL 库的新手,需要帮助。 我想添加两个数字,假设 A = 4555 和 B = 50,并将它们输出为: 4555 +50 4605 另一个例子: 500000 + 12 500000
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我是一名优秀的程序员,十分优秀!