在不更改计数函数的情况下将整数更改为货币格式
changing an integer to money format without changing the cout function
我有一个整数的汽车成本,我需要在货币格式3700.00显示然而,我们不应该改变它打印的代码。
TODO: Make the quote lines below print to look like money
// Example 3.45234 should be --> 3.45
// Think about iomanip
// ---------------------------------------
// Put some code in here
// ---------------------------------------
static_cast<double>(carCost);
static_cast<double>(upgradeCost);
// ---------------------------------------
// ============================================================
// Don't mess with these lines ================================
// ============================================================
std::cout << "[" << quoteNumber++ << "] "; // Don't touch me
std::cout << " Car($" << carCost << ")"; // Don't touch me
std::cout << " E(" << engineLevel << ")"; // Don't touch me
std::cout << " T(" << tireLevel << ")"; // Don't touch me
std::cout << " R(" << rimLevel << ")"; // Don't touch me
std::cout << " P(" << paintLevel << ")"; // Don't touch me
std::cout << " M(" << mufflerLevel << ")"; // Don't touch me
// Don't touch the following line -- HOWEVER, you should put
// something in the space ABOVE to make this line print like money
std::cout << " Upgrades($" << upgradeCost << ")" << std::endl; // Don't touch me
// ============================================================
我尝试使用static_cast来改变它们,但仍然不能解决如何在不触及cout行的情况下改变格式。
将#include <iomanip>
添加到您的#include
中,您可以使用
std::cout << std::fixed << std::setprecision(2);
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 在一定长度后从数组中打印时缺少整数
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- std::当在256字节边界上写入整数时,流的奇怪行为
- C++嵌套if语句,基本货币交换
- 在不更改计数函数的情况下将整数更改为货币格式