Decimals not rounding properly

I have a decimal field that holds a percentage value, such as 37.5 or 62.5

I convert that to an integer to remove the decimals. However, it isn't rounding up and down properly when it is .5

37.5 rounds up to 38.
62.5 rounds down to 62.
87.5 rounds up to 88.

Everything else works as expected.

Anyone know why this is and how to fix it?

Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
You need

37.5 -> 37 for this ??

In maths when the first number after the decimal point is 5 and since it is is greater than 4 so our rounded number will be 37 + 1 = 38.
Pawan KumarDatabase ExpertCommented:
edited my first comment.
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
I found out that does what is called banker's rounding.

The theory is that if every .5 is rounded up, your results will eventually be off, too high. So it rounds some less and some higher. I don't know the specific rules, but found the overview explanation.

Here is an article about banker's rounding:

If I want to override it I can use Math.Round with an overload for MidpointRounding.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

I cannot reproduce your issue:
using System;
using System.Linq;

namespace EE_Q29066147
    class Program
        static void Main(string[] args)
            var decimals = new[] { 37.5m, 62.5m, 87.5m };
            Console.WriteLine(string.Join(Environment.NewLine, (from d in decimals
                                                                select $"{{{d} rounded equals {Math.Round(d, MidpointRounding.AwayFromZero)}}}")));

Open in new window

Produces -Capture.PNG
What are your actual values and how are you rounding?

Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
I should give it_saige some points since he was likely formulating his tests and response when I found the solution to be to use MidpointRounding.AwayFromZero.
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Complete answer and explanation why it doesn't work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.