+ Reply to Thread
Results 1 to 10 of 10

Round Inconsistency

  1. #1
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Round Inconsistency

    On my Excel 07, the worksheet round function rounds 4.5 (to zero decimal places) to 5. In vba, the round function rounds 4.5 (to zero decimal places) to 4. What?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

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

    re: Round Inconsistency

    VBA Round uses banker's rounding; the ROUND function does not.

    The worksheet ROUND function is available to VBA.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    re: Round Inconsistency

    Well, I'm glad that was common knowledge to you. I have to go back through a project I'm working on and reload a bunch of data into my workbook because I assumed they would be the same. I've lost out on a half day of work and I looked like an ametuer to my coworkers.

    PS, I work in a bank but I still had to look up what banker's rounding was. I'm surprised anyone would ever want that. How would that be programmatically advantageous?

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

    re: Round Inconsistency

    See http://en.wikipedia.org/wiki/Roundin...d_half_to_even

    Note particularly,

    It is the default rounding mode used in IEEE 754 computing functions and operators (and in various computing languages such as ANSI/ISO C, C++, and Java, for their float and double types).
    At a glance, I can't find confirmation of any those statements, but the intent is to reduce bias when you're summing a lot of rounded numbers.

  5. #5
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Round Inconsistency

    Interesting, I can see why it would be valuable for reducing bias. I guess that was something I would have learned if I had actual programming education. Thanks shg.

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Round Inconsistency

    In VBA I rarely use the round function, I'd normally use

    Please Login or Register  to view this content.

    Or

    Please Login or Register  to view this content.

    Depending on how I wanted to round. I suppose it's a hangover from coding in the days before there was a round function, but it does mean that I don't get caught out with unexpected values being returned.

  7. #7
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Round Inconsistency

    Worth mentioning that things like 10*int(x/10) can be used to round to any kind of decimal precision, like round, and more to the point, things like 5*int(x/5) can be used to round to any interval.

    (not that you guys didn't know that, just in case someone chances on the thread!)
    CC


    If you feel really indebted please consider a donation to charity. My preferred charity is ActionAid but there are plenty of worthy alternatives.

  8. #8
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Round Inconsistency

    I'd normally go with

    Please Login or Register  to view this content.
    Where r is the number of decimal places required.

  9. #9
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Round Inconsistency

    Please Login or Register  to view this content.
    This is different from Excel's normal rounding procedure. Excel normally uses symetrical rounding around 0, whereas this produces asymetrical rounding.

    For example, -1.2345 rounded to 3 decimal places;
    Using ROUND(-1.2345,3) gives -1.235 which is rounding Down

    The formula above gives -1.234 which is rounding Up (which is what you would expect when the next and final digit is 5).

    As most of my worksheets are made for the workplace, it's important to know what their rounding policies are.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  10. #10
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Round Inconsistency

    I started playing with this last night and it seems to expose one of Excel's little problems.

    If you start with the value 1.2345 and put it into my rounding function with r=3 then Excel reports

    x * 10^3 = 1234.5
    x * 10^3 + 0.5 = 1235
    Int(x*10^3+0.5) = 1234
    Int(x*10^3+0.5)/10^3 = 1.234

    So there's a calculation error between steps 2 and 3 that seems linked to how x and r are defined in the function. If I set them both as variants then it works fine, if I define x as a double and r as an integer it gives the error above.

    Any thoughts?

+ 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