Losing my precision
A colleague of mine ran into an issue with NUnit today. He was comparing a decimal value returned from a database query with the expected value, and couldn't figure out why his assertion kept failing, even though he was sure both values were equal. We looked a bit closer and saw that the expected value was 20, but the returned value was 20.0000. If you call Decimal.Equals() on those two values, it will return true, because it ignores trailing zeros. NUnit, however, calls ToString() on numeric types before it compares them, so the comparison obviously fails.
This brought up another question: why is there no method on Decimal to compare both the value and the precision of two decimal numbers? In Java, the BigDecimal class provides for it with the CompareTo() method, but Decimal.CompareTo() in .Net behaves pretty much the same as Equals(). Surely in some cases 20 should not be considered equal to 20.0000?
The only way I can see to compare value and precision is by calling Decimal.GetBits() with both values, and stepping through the two arrays.
Interesting, n'est-ce pas?