我使用FloatToStr()但有错误!
我告诉你我的源代码。

void __fastcall TForm1::FormCreate(TObject *Sender)
{
float i=3.14;
 Double j=3.14;
 Double k=0;
 Double m,n;

 Edit1->Text=FloatToStr(i);         //  It's show 3.14000010490417
 Edit2->Text=FloatToStr(j);         //  It's show 3.14

 Edit3->Text=FloatToStr(314/100);   // It's  show 3

 k=314/100;
 Edit4->Text=FloatToStr(k);         // It's show  3

 m=314;
 n=100;
 Edit5->Text=FloatToStr(m/n);       // It's  show 3.14

}


我问 ?为什么呢全部不显示3.14吗??? !!!或者这是FloatToStr()中的错误!


Edit1-> Text = FloatToStr(i); //显示3.14000010490417
Edit3-> Text = FloatToStr(314/100); //显示3
Edit4-> Text = FloatToStr(k); //显示3


感谢您的回答。

最佳答案

您得到不同的结果,因为您在代码中混合了整数除法和浮点除法。整数除法会截断小数点后的所有内容。

// This line is performing integer division
// and the final result is passed into FloatToStr.
// aka it's the same as calling FloatToStr(3).
Edit3->Text=FloatToStr(314/100);

// Again, the right-hand side is doing integer division
// and the result is implicitly casted to a double.
// aka it's the same as doing k = static_cast<double> (3);
k = 314/100;
Edit4->Text=FloatToStr(k);         // It's show  3

// These two lines implicitly casts int to a double
// when assigned in this manner.
m = 314;
n = 100;
// Now this is performing double division so you
// get the expected result. It's the same as calling
// Edit5->Text=FloatToStr( 314.0/100.0 );
Edit5->Text=FloatToStr(m/n);       // It's  show 3.14

10-04 14:15