I need to compare the integer part of two doubles for inequality and I’m currently doing this:
int iA = (int)dA; int iB = (int)dB; if( iA != iB ) { ... }
but I wonder if there’s a better approach than this.
Thanks.
If I used Math.Truncate() instead of a cast to int, would it still be accurate to compare the two resulting double values for equality?
About the hungarian notation comments:
I never use HN myself, not at least in the way most of people do. But this is one of these rare cases where the semantic of a variable directly relates to its type. I could have chosen another syntax like A_As_Integer and B_As_NonInteger but what would have been the difference then?
Use Math.Truncate() i.e.
[Edit] Realized that if you are comparing integer parts of doubles, casting to int values first runs the risk of overflows should your doubles be outside the range that could be represented as int.
Truncate returns either a Decimal or a double, avoiding this issue.