What's the best way to compare Double and Int?

前端 未结 6 1824
既然无缘
既然无缘 2020-11-29 10:43

The following code in C# doesn\'t work:

int iValue = 0;
double dValue = 0.0;

bool isEqual = iValue.Equals(dValue);

So, the question: what\

6条回答
  •  一向
    一向 (楼主)
    2020-11-29 11:38

    From https://docs.microsoft.com/en-us/dotnet/api/system.double.equals?view=netframework-4.8#System_Double_Equals_System_Double_

    Because Epsilon defines the minimum expression of a positive value whose range is near zero, the margin of difference between two similar values must be greater than Epsilon. Typically, it is many times greater than Epsilon. Because of this, we recommend that you do not use Epsilon when comparing Double values for equality.

提交回复
热议问题