在不更改计数函数的情况下将整数更改为货币格式

changing an integer to money format without changing the cout function

本文关键字:整数 货币 情况下 格式 函数      更新时间:2023-10-16

我有一个整数的汽车成本,我需要在货币格式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);