Solved

VBA Rounding Issue

Posted on 2011-03-09
13
728 Views
Last Modified: 2012-06-10
in VBA i used Round() function to round off a number to 2 decimal.

For 2.855 the 2 decimal rounding should be 2.86, however using Round(2.855,2) gives 2.85 !!!

How to get round half up working ?

Help appreciated, Thanks.
0
Comment
Question by:cynx
  • 4
  • 3
  • 2
  • +1
13 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35083988
Use Application.Round to get the worksheet function version of Round rather than the VBA Round.
0
 
LVL 33

Accepted Solution

by:
jppinto earned 167 total points
ID: 35084001
Use the Application.Round function instead, like this:

Application.Round(2.855, 2)

jppinto
0
 
LVL 9

Expert Comment

by:felixdsouza
ID: 35090671
I just checked and found that VBA rounding as mentioned in the question works just fine.

I am attaching a sample XL file with the code here.  Please check it out and let me know.

 VBA-Rounding.xls
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 167 total points
ID: 35093241
VBA's Round function uses banker's rounding, the worksheet function ROUND uses arithmetic rounding. (the example given by the asker I suspect was a typo)
In a worksheet =ROUND(2.865,2) will give 2.87
In VBA Round(2.865,2) will give 2.86

If you want arithmetic rounding (consistent with the worksheet functions) you need either Application.Round or Application.WorksheetFunction.Round

Regards,
Rory
0
 
LVL 9

Assisted Solution

by:felixdsouza
felixdsouza earned 166 total points
ID: 35093342
Ok, rorya,

I agree with you on the point that Application.Round or Application.Worksheetfunction.Round needs to be used.

However, please look at the attached file:
VBA round correctly computes Round(2.855,2) as 2.86 and Round(2.875,2) as 2.88.

I suspect VBA alternates between rounding up and rounding down... where the last significant digit to be rounded off (in this case the second decimal digit) is odd, it rounds up (arithmetic rounding, which is want we want and expect), and where that particular digit is even, VBA round down.

I have attached sample file.
 VBA-Rounding-2.xls
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35093366
That's what I said - it's called Banker's Rounding. :)
0
 
LVL 1

Author Comment

by:cynx
ID: 35344130
sorry was away for a while, missed out on assigning points.
rorya's first post solved my issue, hence all 500 for him :), thanks all for help.
0
 
LVL 1

Author Comment

by:cynx
ID: 35344160
I am not able to allocate points as this was abandoned.
i get following error
This question already has an auto close request. You must cancel the previous request before creating another.

all 500 points should rightly be assigned to rorya. mods plz help sorting this out.
0
 
LVL 1

Author Comment

by:cynx
ID: 35344170
I am not able to allocate points as this was abandoned.
i get following error
This question already has an auto close request. You must cancel the previous request before creating another.

all 500 points should rightly be assigned to rorya. mods plz help sorting this out.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35359557
Somebody care to explain this allocation which goes directly against what the asked wanted?
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
EXCEL 2013 question. 4 28
Excel Spacing Anomaly 4 23
Activation for Microsoft Office for Mac 2016 3 38
Clear Filter 8 40
Outlook Free & Paid Tools
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question