How to properly compare decimal values in C#?

后端 未结 4 739
忘掉有多难
忘掉有多难 2020-12-16 09:23

I come from a background in C++, and I know that you cannot accurately compare floats for equality. For C#, I simply assumed the same policy applies to decimal values, or an

4条回答
  •  执念已碎
    2020-12-16 09:49

    Your code will work as expected. C# decimals are optimized to be very accurate at representing base 10 numbers, so if that's what you're comparing (money, ...), everything should be fine.

    Here's a very clear explanation about the accuracy of decimals by Jon Skeet:

    Difference between decimal, float and double in .NET?

提交回复
热议问题