Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

VBA Rounding Issue

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
cynx
Asked:
cynx
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
Rory ArchibaldCommented:
Use Application.Round to get the worksheet function version of Round rather than the VBA Round.
0
 
jppintoCommented:
Use the Application.Round function instead, like this:

Application.Round(2.855, 2)

jppinto
0
 
felixdsouzaCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Rory ArchibaldCommented:
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
 
felixdsouzaCommented:
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
 
Rory ArchibaldCommented:
That's what I said - it's called Banker's Rounding. :)
0
 
cynxAuthor Commented:
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
 
cynxAuthor Commented:
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
 
cynxAuthor Commented:
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
 
Rory ArchibaldCommented:
Somebody care to explain this allocation which goes directly against what the asked wanted?
0
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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now