C++ 为什么这个函数不输出小数
我有一个功能:C++ 为什么这个函数不输出小数,c++,C++,我有一个功能: void LocalMax(vector<Station> Entry , int Size) { int Highest1 = 0, Highest2 = 0, Highest3 = 0, Temp = 0, Highest = 0; double TempDouble; for (int i = 0; i < Size - 1; i++) { Temp = Entry[i].MaxTemp; i
void LocalMax(vector<Station> Entry , int Size)
{
int Highest1 = 0, Highest2 = 0, Highest3 = 0, Temp = 0, Highest = 0;
double TempDouble;
for (int i = 0; i < Size - 1; i++)
{
Temp = Entry[i].MaxTemp;
if (Temp > Highest) {Highest = 0; Highest = Temp;}
if (Entry[i].StationID != Entry[(i + 1)].StationID)
{
if (Entry[i].StationID == "GHCND:USC00083909") {Highest1 = Highest; Highest = 0;}
if (Entry[i].StationID == "GHCND:USW00012888") {Highest2 = Highest; Highest = 0;}
if (Entry[i].StationID == "GHCND:USR0000FCHE") {Highest3 = Highest;}
} else if (Temp > Highest) {Highest = Temp; Temp = 0;}
if (i == Size - 2) {Highest3 = Highest;}
}
TempDouble = Highest1 / 10;
cout << "The highest temp recorded for Station1 was: " << Highest1 << " tenths of a degree Celsius \nor: " << TempDouble << " degrees Celsius." << endl;
TempDouble = Highest2 / 10;
cout << "The highest temp recorded for Station2 was: " << Highest2 << " tenths of a degree Celsius \nor: " << TempDouble << " degrees Celsius." << endl;
TempDouble = Highest3 / 10;
cout << "The highest temp recorded for Station3 was: " << Highest3 << " tenths of a degree Celsius \nor: " << TempDouble << " degrees Celsius." << endl;
}
这给了我:
The highest temp recorded for Station1 was: 339 tenths of a degree Celsius
or: 33 degrees Celsius.
The highest temp recorded for Station2 was: 350 tenths of a degree Celsius
or: 35 degrees Celsius.
The highest temp recorded for Station3 was: 344 tenths of a degree Celsius
or: 34 degrees Celsius.
为什么它不显示tempdoull的小数点?任何澄清都很好!提前感谢。您的代码执行整数除法。这是因为两个操作数都是整数
TempDouble = Highest1 / 10;
整数除法产生整数结果。是的,然后将该整数赋给浮点值是正确的,但为时已晚。您已经丢失了除法的小数部分
您需要使至少一个操作数为实数才能得到实除法
例如:
TempDouble = Highest1 / 10.0;
您的代码执行整数除法。这是因为两个操作数都是整数
TempDouble = Highest1 / 10;
整数除法产生整数结果。是的,然后将该整数赋给浮点值是正确的,但为时已晚。您已经丢失了除法的小数部分
您需要使至少一个操作数为实数才能得到实除法
例如:
TempDouble = Highest1 / 10.0;
此行执行整数除法,因为/的左操作数和右操作数是整数:
若要获取双精度,请确保其中一个操作数是双精度的。最简单的方法是:
TempDouble = Highest1 / 10.0;
此行执行整数除法,因为/的左操作数和右操作数是整数:
若要获取双精度,请确保其中一个操作数是双精度的。最简单的方法是:
TempDouble = Highest1 / 10.0;
整数运算的结果是一个整数。如果需要浮点结果,则需要在表达式中包含一个浮点数,例如:
TempDouble = Highest1 / 10.0;
整数运算的结果是一个整数。如果需要浮点结果,则需要在表达式中包含一个浮点数,例如:
TempDouble = Highest1 / 10.0;
非常感谢您的快速回复。我很感激!非常感谢您的快速回复。我很感激!谢谢你的回复!谢谢你的回复!