# 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?

thanks!
LVL 2
###### Who is Participating?
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.

Database 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.
0
Database ExpertCommented:
edited my first comment.
0
ASP.NET VB.NET DeveloperAuthor Commented:
I found out that asp.net 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:
https://www.wyzant.com/resources/blogs/331496/rounding_rules_when_to_round_and_how_do_you_round_5_s_up_or_down

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

thanks!
0

Experts Exchange Solution brought to you by

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

DeveloperCommented:
``````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)}}}")));
}
}
}
``````
Produces -
What are your actual values and how are you rounding?

-saige-
0
ASP.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.
0
ASP.NET VB.NET DeveloperAuthor Commented:
Complete answer and explanation why it doesn't work.
0
###### 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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.