#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float c = 5.0;
float far = (9/5)*c + 32;
cout << fixed << "Temperature in Fahrenheit is "<< setprecision(2) <<
far;
return 0;
}
我预计输出为 41.00,但实际输出为 37.00。
最佳答案
请注意,9
和 5
是 int
,因此 9/5
结果为 int
1
.
您的代码在 float
上运行,并且需要 float
乘数才能使转换正常工作。因此,解决方法是将乘数定义为 9.f/5
(.
是 .0
指数的简写符号, f
后缀指定一个 float
文字,请参阅 floating point literal 了解更多详细信息),例如:
float far = (9.f / 5) * c + 32;
关于c++ - C++ 中的固定和设置精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57713990/