我试图将我的最终答案四舍五入为两位小数,所以它是美元和美分。我是编码的新手,无法弄清楚。我想在“w需要收取的金额为”行中四舍五入“w”,这是我的代码:

#include <iostream>

using namespace std;

int main()
{
    string Choice;


    float x, w;

    cout << "Please enter the amount needed." << endl;
    cin >> x;


    w = x/(1-0.0275);

    cout << "The amount you need to charge is $"<< w << "." << endl;

    return (0);

}

最佳答案

根据此处的示例http://www.cplusplus.com/forum/beginner/3600/
你可以用

cout << setprecision(2) << fixed << w << endl;

(fixed是可选的)

您将必须#include <iomanip>
正如Synxis指出的那样,这仅适用于打印值,而不会更改w持有的值

关于c++ - 希望将最终答案四舍五入为C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15189084/

10-11 23:04
查看更多