2
Reply

strange decimal rounding

George George

George George

Jun 12 2008 1:24 AM
2k

Hello everyone,


As mentioned here,

http://msdn.microsoft.com/en-us/library/system.decimal(VS.80).aspx

"Conversions from Decimal to other types are narrowing conversions that round the Decimal value to the nearest integer value toward zero."

It conflicts my previous understanding that in all rounding in C#, we round to the nearest integer. Here is my test code. How strange it is, right?

[Code]
using System;

class Test
{
    static void Main()
    {
        Decimal b = 100.9M;
        int a = (int)b;

        // output 100, other than 101
        Console.WriteLine(a);

        return;
    }
}
[/Code]


thanks in advance,
George


Answers (2)