寻找最后回答2小数C ++ [英] Looking to round the final answer to 2 decimals C++

查看:127
本文介绍了寻找最后回答2小数C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的最后答案舍入为2小数,所以它是美元和美分。我是新的编码,并不能弄清楚。我想在你需要收费的金额是这一行的w这里是我的代码:

I'm trying to round my final answer to 2 decimals so it is Dollars and Cents. I'm new to coding, and can't figure it out. I want to round "w" in the line that says "The amount you need to charge is" Here's my code:

#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/
您可以使用

According to the example here http://www.cplusplus.com/forum/beginner/3600/ You could use

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

固定是可选的)

您必须 #include< iomanip>

正如Synxis所指出的,这只能用于打印值,它不会改变 w

As pointed out by Synxis, this will only work for printing the value, it will not change the value held by w

这篇关于寻找最后回答2小数C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆