Denis Denis - 1 year ago 76 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 Source

If you want classic rounding use

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

Please see;k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5.2);k(DevLang-csharp)&rd=true for details

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download