在不更改cout函数的情况下将整数更改为money格式

时间:2015-09-21 02:42:29

标签: c++

我有一个整数carCost,我需要以货币格式显示,即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行的情况下更改格式。

1 个答案:

答案 0 :(得分:0)

#include <iomanip>添加到#include,您可以使用

std::cout << std::fixed << std::setprecision(2);

行之前。