Denis Denis - 1 month ago 9
Vb.net Question

Why does the decimal 2.5M round to 2?

I must be missing some subtlety of .NET rounding. So I am looking at this example:

decimal num = 2.5M;
var result = Math.Round(num);


Why is
result = 2
? (I would have expected 3 since it should round up)

Answer

If you want classic rounding use

        decimal num = 2.5M;
        var result = Math.Round(num,0, MidpointRounding.AwayFromZero);

Please see https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(System.Math.Round);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5.2);k(DevLang-csharp)&rd=true for details