C++ 将十进制四舍五入到第十位,但在c+中显示第一百位+;
我做这项家庭作业已经有一段时间了,我准备把我的头发拔出来 我需要帮助将一个浮点数四舍五入到第十位,同时在第一百位仍然显示0,而我所做的一切似乎都没有做到这一点。 i、 e.2.47=2.50C++ 将十进制四舍五入到第十位,但在c+中显示第一百位+;,c++,decimal,iomanip,C++,Decimal,Iomanip,我做这项家庭作业已经有一段时间了,我准备把我的头发拔出来 我需要帮助将一个浮点数四舍五入到第十位,同时在第一百位仍然显示0,而我所做的一切似乎都没有做到这一点。 i、 e.2.47=2.50 #include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; int main() { float MPH; float seconds; const float MPH2MPS =
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float MPH;
float seconds;
const float MPH2MPS = (1609.00 / 3600.00);
float a;
cout << " Acceleration calculator" << endl;
cout << "" << endl;
cout << "Please enter the velocity in miles per hour: ";
cin >> MPH;
cout << "" << endl;
cout << "Please enter the time in secounds: ";
cin >> seconds;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;
a = MPH2MPS * ( MPH / seconds );
cout << showpoint << fixed << setprecision(2);
cout << "The acceleration required by a vehicle to reach" << endl;
cout << "" << endl;
cout << "a velocity of " << MPH << " miles per hour in " << seconds << " seconds" << endl;
cout << "" << endl;
cout << "is " << setprecision(1) << a << " meters per second" << endl;
cout << "" << endl;
cout << "" << endl;
system("pause");
return 0;
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int main()
{
浮动时速;
浮动秒;
常量浮点MPH2MPS=(1609.00/3600.00);
浮动a;
阅读@asterite的答案:
不欢迎使用Stack Overflow。请花点时间浏览并参考中的资料,了解您可以在此处询问什么以及如何提问。发布一条特别重要的信息。请显示代码。并告诉我们您是要对变量本身进行四舍五入,还是要对显示进行四舍五入。这里的明显选择是实现您的own舍入函数…你能告诉我你使用的是什么值作为输入吗?不管出于什么原因,舍入的可能重复项在包括数学库之后也不起作用。它对我有效,提示级别为100,秒为10,我得到4.50,即4.47舍入到第十位,后面是0。如果我误解了什么,请告诉我。我不知道我不知道为什么,但它声称round不是“std”的成员,并且找不到标识符。您需要包含cmath库。如果未正确包含该库,则会显示该错误
cout << "is " << static_cast<double>(std::round(a * 10)) / 10 << " meters per second" << endl;