.Net Rounding issue (MidpointRounding)

Issue:

.Net rounding does not work as you would expect. -2.5 rounds to -2 and not -3. What?! Yes! By default .Net uses MidpointRounding.ToEven which will round it to -2. BUT if you round off -3.5 it works as you would expect to -4. If you round off -6.5 you think it would also round off to -7, but you would be wrong, -6! What about 2.5, the default rounds off the 2 and not 3 as you would expect.

So what is going on? You can read more about it at http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx.

The fix below is to make sure it ALWAYS rounds off the same way.

Fix:

To make sure it consistently rounds off the same way, use the .Net enum MidpointRounding:

var amount = Math.Round(notRoundedDecimalValue, 0, MidpointRounding.AwayFromZero);

This will make sure -2.5, rounds to -3

Some other examples:

base           rounded to 0 decimals after comma

-1.5            -2 (same for both))

-2.5            -3 (default -2)

-3.5            -4 (same for both))

-4.5            -5 (default -4)

-5.5            -6 (same for both))

-6.5            -7 (default -6)

-7.5            -8 (same for both)

1.5            2 (same for both)

2.5            3 (default 2)

6.5            7 (default 6)

So to make sure it consistently rounds of the same way regardless of the number range you are in use:

MidpointRounding.AwayFromZero

.Net - find business days

Goal:

Find the business days; i.e. exclude weekends

How to:

Here is some code to get the first business day after or before the weekend. It is surprisingly simple but thought the code may help save folks some time. Just a note that there is no built-in .Net feature to give you public US or other holiday calendar dates, you need to build your own using a table to keep track of public holidays in most situations. Simple enough to do, extend the methods below to query a database or xml or JSON, etc, file with your company or country holiday calendar.

 

    public static class DayOfWeekUtility
    {
        public static DateTime GetFirstBusinessDayAfter(DateTime baseDate)
        {
            var businessDate = baseDate;
            var dayOfWeek = businessDate.DayOfWeek;
           
            if (dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday)
            {
                if (dayOfWeek == DayOfWeek.Saturday)
                    businessDate = businessDate.AddDays(2);
                if (dayOfWeek == DayOfWeek.Sunday)
                    businessDate = businessDate.AddDays(1);
            }
            return businessDate;
        }

        public static DateTime GetFirstBusinessDayBefore(DateTime baseDate)
        {
            var businessDate = baseDate;
            var dayOfWeek = businessDate.DayOfWeek;

            if (dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday)
            {
                if (dayOfWeek == DayOfWeek.Saturday)
                    businessDate = businessDate.AddDays(-1);
                if (dayOfWeek == DayOfWeek.Sunday)
                    businessDate = businessDate.AddDays(-2);
            }
            return businessDate;
        }

    }

 

 

Twitter