As of .NET Core 3.0 is "MidpointRounding" a misnomer?

Viewed 70

.NET contains an enum to specify midpoint rounding modes:

  • AwayFromZero (Available since .NET Core 1.0)
  • ToEven (Available since .NET Core 1.0)
  • ToNegativeInfinity (Available since .NET Core 3.0)
  • ToPositiveInfinity (Available since .NET Core 3.0)
  • ToZero (Available since .NET Core 3.0)

It seems that some of these enum values are used to determine how to round midpoint values, whilst others apply even when not rounding by the midpoint.

Consider the following code:

for (decimal value = 1.0m; value <= 3.0m; value += 0.1m)
{
    MidpointRounding[] roundings = Enum.GetValues<MidpointRounding>();

    foreach (MidpointRounding rounding in roundings)
    {
        Console.WriteLine($"{value} {rounding} = {Math.Round(value, rounding)}");
    }
}

This creates a lot of output, which would unnecessarily bloat this question, so I've highlighted a few notable outputs:

value rounding mode = rounded value

1.1 ToPositiveInfinity = 2

1.9 ToNegativeInfinity = 1

Is seems then that MidpointRounding is a misnomer, since it contains rounding modes that do not operate on the midpoint.

Would this be a correct observation?

0 Answers
Related