Posts

Showing posts from August, 2011

The woes of Silverlight binding to a decimal type.

Update 10/2016 - This posting is better viewed in my new blog . I came across something that grabbed my attention the other day.  I was working with a Silverlight application and had bound TextBox controls to two separate decimal properties of a ViewModel class.  I was using the default formatting for display and was quite perplexed when I found that even though both properties had the same value, i.e. 100, one TextBox displayed 100.00 and the other 100.0000.  After doing a little digging I found that the root cause of this behavior is that decimal types in .NET carry not only a value but that value's precision as well (apparently since .NET 1.1).  To see this in action take this case: decimal d1 = 100; decimal d2 = 100.00M; Console.WriteLine( "d1: {0}, d2: {1}, Equal={2}" , d1, d2, d1 == d2); The result will be the output "d1: 100, d2: 100.00, Equal=true" which is to say that the two variables have equal values but different precisions and thus the T