+ Reply to Thread
Results 1 to 20 of 20

VBA Bankers Rounding problem

  1. #1
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    VBA Bankers Rounding problem

    Hi,

    I'm trying to use the VBA Round() function in excel but I'm getting odd results sometimes. Example below.

    Number to round to 7 decimal places: 0.71263125

    Using the VBA Round() function returns 0.7126313 which is incorrect.
    It should be 0.7126312.

    However what's really strange is that if I change the original number to
    0.71263225 (i.e. the 6th number), I get the correct answer of 0.7126322.

    Two other examples of it not working correctly are (rounding to 7):
    2.27683045 rounds to 2.2768305
    3.00487565 rounds to 3.0048757


    Any ideas why this is happening?
    Thanks.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: VBA Bankers Rounding problem

    Hi,

    I believe bankers rounding rounds up when the rounded figure is 5 or above?
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: VBA Bankers Rounding problem

    Not exactly. It rounds up when the digit before the 5 is odd. If it's even it rounds down.

    Example

    1.1235 rounds to 1.124
    1.1225 rounds to 1.122

    However the examples I've shown in my original post rounds incorrectly.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: VBA Bankers Rounding problem

    I would suspect it's an issue with the binary representation of the decimal value. It may be that those numbers are in fact internally represented as 0.712631250000001 for example, so the banker's rounding is doing what it should.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: VBA Bankers Rounding problem

    I've just checked that. I set the decimal place number to 30 in the cell

    0.712631250000000000000000000000

    So it doesn't look like there's a 1 at the very end.

    The Round() function failed for 3 out of 112 random numbers so it's a very small example I'm dealing with that I can't figure out why. It doesn't make any sense why it wouldn't be working for these numbers

  6. #6
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Post Re: VBA Bankers Rounding problem

    I believe that Excels 'round' function just uses standard 'rounding rules', and it is rounding perfectly as expected.

    The standard rounding rules are that, for example, if you are rounding to 7 decimals;
    If the 8th decimal is 5 or above the 7th decimal is increased by one
    If the 8th decimal is 4 or less the 7th decimal remains as it was.

    Which is not 'Bankers Rounding' which is why your values are 'incorrect'.

    In Bankers Rounding I believe that if the 8th decimal is a 5, then if the 7th decimal is odd it is rounded up, and if it is even it is rounded down, (so the 7th decimal is effectively rounded to it's nearest even denomination. - Do a 'Find' on this wiki page for 'Banker' for the full description: http://en.wikipedia.org/wiki/Rounding )
    This means there are a few things you need to check before you do your rounding;
    - Firstly is the 8th decimal a 5? If not just round normally
    - Secondly, if it is a 5 then the rounded figure will depend on whether the 7th decimal is odd or even.

    With that in mind try the following code in a cell, (presuming the value that you want to round is in A1):
    Please Login or Register  to view this content.
    Note
    BLUE references are the number you want to round.
    RED digits are the number of decimals you will round to.
    DARK RED digits are the number of decimals you will round to +1.

    Please try this on a worksheet and see if it gives the results you expect.
    If it works and you need it as VBA code let me know
    Last edited by Phil_V; 08-20-2009 at 06:08 AM. Reason: Added wiki link
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  7. #7
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: VBA Bankers Rounding problem

    I'm afraid the MROUND() function doesn't work. I'm getting a #NAME? error. Just found the reason.
    "If this function is not available, and returns the #NAME? error, install and load the Analysis ToolPak add-in." Not sure if I can get this installed on my work machine. Hang on, got it installed. I'll check it out again. But I see what you're doing with the formula.

    I'd like it in VBA code too if that's ok.

    You mentioned "- Firstly is the 8th decimal a 5? If not just round normally"
    Some of the numbers will have 8, 9 or 10 decimal place with the intention of rounding to 7. So will your formula still work if it only checks if the 8th digit is 5?
    Can you update the formula to let's say check all the digits after the 7th decimal place and if the result is 5 and only 5, i.e. not 50001, then use the correct rounding?

    If the number is say 1.1234566595, this would round to 1.1234567
    Last edited by sflemings; 08-20-2009 at 06:51 AM.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: VBA Bankers Rounding problem

    The difference is due to binary numbers though - you wouldn't see it just from typing values into cells as Excel will compensate. Try this test:
    Please Login or Register  to view this content.
    You will note that dblDiff1 is smaller than dblDiff2, and hence the rounding goes up. It is doing Banker's rounding as expected, it's just that the values it's working with are not quite what you think they are!

    X-post here, BTW.
    Last edited by romperstomper; 08-20-2009 at 09:13 AM.

  9. #9
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: VBA Bankers Rounding problem

    Thanks for that. This is happening very few times but it's obviously the same reason.

    Is it possible to take the Math.Round() VB formula and stick it in as a macro? Would that make any difference or would it just be the same result.

    Can you come up with a formula to take all the digits after the 7th decimal place, check is this is exactly equal to 5, if so check the 7th digit if it's odd or even and perform the rounding after that?

  10. #10
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,792

    Re: VBA Bankers Rounding problem

    This formula should work for you, without using MROUND

    =ROUND(A2,7)-(MOD(ROUND(A2*10^(7+1),0),20)=5)/10^7

  11. #11
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: VBA Bankers Rounding problem

    Quote Originally Posted by sflemings View Post
    You mentioned "- Firstly is the 8th decimal a 5? If not just round normally"
    Some of the numbers will have 8, 9 or 10 decimal place with the intention of rounding to 7. So will your formula still work if it only checks if the 8th digit is 5?
    Can you update the formula to let's say check all the digits after the 7th decimal place and if the result is 5 and only 5, i.e. not 50001, then use the correct rounding?
    The first thing my code does is round the number to 8 decimal places using normal rounding, and then inspects the last digit, and performs the neccasary rounding function based on that, so it shouldn't matter about having additional decimal places.

    The best thing is for you to try it and see if it gives the results you expect. It seems to work fine here, but I am no Banker

  12. #12
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: VBA Bankers Rounding problem

    Quote Originally Posted by sflemings View Post
    Is it possible to take the Math.Round() VB formula and stick it in as a macro? Would that make any difference or would it just be the same result.
    It would give the same result.
    The ROUND function is not broken! It is doing exactly as it is supposed to, it is rounding using conventional rulings.
    What you are wanting though is BANKERS ROUNDING, which is not what the ROUND function does, hence you get the 'wrong' result.
    Thats why I had to generate a custom formula for you.

    If you want it in VBA format, (and also don't want to need the Analysis toolpak), then paste the following code into a VBA module in your workbook:
    Please Login or Register  to view this content.
    You can then use it in your worksheet like:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Or you can use it in your vba code:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Try it and let me know how you get on please

  13. #13
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: VBA Bankers Rounding problem

    I know I want Bankers Rounding! That's what I've been trying to get all along. I know that the excel Round() function is different to the VBA Round() function.

    Your code works fine for values that ends in 5 after the initial rounding.
    This example below doesn't work with it though, rounding to 7 decimal places.
    0.712631251

    This should be rounded to 0.7126313 but your code is rounding it to 0.7126312 because it's firstly rounding to 8 which is 0.71263125, and then rounds this down to 0.7126312

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: VBA Bankers Rounding problem

    Try this:
    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Unhappy Re: VBA Bankers Rounding problem

    In that case I applogise as it seems I didn't fully understand how the rounding of the additional decimals should be processed.
    Romper's code seems to work well on the numbers I have tested, and it is also simple and elegant, (so much so that I am now going to sit and try to work out exactly what it is doing )

    EDIT

    It appears that I also didn't realise that the VBA Round function behaves differently to the worksheet Round() function
    Not my day today
    Can I ask, where does it describe the difference between the worksheet round function, and then VBA round function? I'm wondering now where else I may have made mistakes in my own code due to this
    Last edited by Phil_V; 08-20-2009 at 02:48 PM.

  16. #16
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: VBA Bankers Rounding problem

    romperstomper, that code seems to work perfect. I think the CDec function sorts it out by changing the value to a decimal before it does the rounding. Thanks.

  17. #17
    Registered User
    Join Date
    08-20-2009
    Location
    Kerry, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: VBA Bankers Rounding problem

    Phil_V, check this link out for an explanation of Microsofts rounding

    http://support.microsoft.com/kb/196652

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA Bankers Rounding problem

    I think this is kind of an academic exercise, considering that your computer stores numbers in binary floating point (IEEE 754-1985) format rather that decimal floating point (IEEE 854, or IEEE 754-2008). For example,
    Please Login or Register  to view this content.
    ... stops at

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  19. #19
    Registered User
    Join Date
    05-06-2014
    Location
    Los Osos, CA
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA Bankers Rounding problem

    I'm trying to use bankround in VPA

    Function BankRound(num, dp) As Double
    BankRound = Round(CDec(num), dp)
    End Function

    It works great until I save the file and reopen it, then I get #NAME? I'm saving the file as a Macro Enable Excel Workbook. What am I doing wrong?

  20. #20
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA Bankers Rounding problem

    hyt,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1