Haven't blogged in a long time, but thought I'd break the silence with a little bug I found in the .NET Framework 1.1. I stand to be corrected on this, but I think a bug exists in the conversion from SqlDecimal to SqlInt32. To demonstrate, create a WinForms app and add two buttons. Use the following code for their click events:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
SqlDecimal sqlDec = new SqlDecimal(decimal.Parse("20,000.50"));
int intValue = sqlDec.ToSqlInt32().Value;
MessageBox.Show(intValue.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
SqlDecimal sqlDec = new SqlDecimal(decimal.Parse("20,000.50"));
decimal dec = sqlDec.Value;
int intValue = (int)dec;
MessageBox.Show(intValue.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Now, I would expect both of these to return the same value of “20000” in the MessageBox, but they don't! It seems that the SqlDecimal conversion to SqlInt32 completely disregards the decimal point and returns the whole value as an int. In other words “2000.50” will convert to “200050” and “200.5000” will convert to “2005000”. I think this is wrong, as a conversion from SqlDecimal to SqlInt32 should truncate the decimal value to make it a whole number.
Any thoughts????
PS... I called Microsoft PSS and was told they don't support free downloads, including the .NET Framework. I've taken this up a few levels and will let you all know what happens!