Monday, July 13, 2009

Truncation and Rounding in Conversion

Just found this. Feels angry:


decimal d = 1.7M;
Console.WriteLine((long)d);
Console.WriteLine(Convert.ToInt64(d));


Yields:

1
2

Ouch.

Remember, kids, casting syntax truncates, while using the Convert class rounds.