Decimal Rounding Tasks

Given that Decimal.MaxValue = 79228162514264337593543950335m

Why is the following line giving me 7922816251426433759354395034M in the local window instead of 7922816251426433759354395033.5m as expected?

Decimal Target = Decimal.MaxValue / 10m;

+5
source share
2 answers

I suspect this is a compiler error.

Here is a short but complete program to show why I think:

using System;

class Test
{
    static void Main()
    {
        decimal constant = decimal.MaxValue / 10m;
        decimal calculated = decimal.MaxValue;
        calculated /= 10m;

        Console.WriteLine (constant);
        Console.WriteLine (calculated);        
    }
}

Output:

7922816251426433759354395034
7922816251426433759354395033.5

I will delve into the specifications to find out what guarantees are given.

EDIT: In the specification, section 7.18:

, , , .

. .

EDIT: Microsoft Connect. , .

+6

All Articles