Rounding

I see Microsoft Access is not consistent in rounding. Interesting I know...I am trying to do banker's round ie. round(1.235)=1.24

If you want to see the description of banker's rounding:
http://support.microsoft.com/default.aspx?scid=kb;en-us;196652

which is round it to nearest even number...

I need two digits after period just like I have above.
Thanks

You can use this syntax to test in VBA:

Debug.Print Round(2.155, 2)
'I need this result to be 2.16
LVL 1
Midnight2005Asked:
Who is Participating?
 
Leigh PurvisConnect With a Mentor Database DeveloperCommented:
Round is a VBA function (as opposed to belonging to Access).

It *should* follow Banker's rounding.  But I can imagine the unreliability of that from region to region.
You could make a function to force it...

Function fRound(varVal, intDP As Integer)
   
    If IsNull(varVal) Then Exit Function
   
    fRound = Round(Val(varVal * (10 ^ intDP))) / (10 ^ intDP)
   
End Function
0
 
GRayLCommented:
It does round correctly

? round(2.155,2)
 2.16

? round(2.145,2)
 2.14

if you want it to alway round up

? round(2.145+.0001,2)
 2.15

ie.  add a very small amount less that the number of your significant decimals
0
 
Eric ShermanAccountant/DeveloperCommented:
According to the Knowledge Base article you posted ....

In Visual Basic for Applications, the following numeric functions perform banker's rounding: CByte(), CInt(), CLng(), CCur(), and Round().


ET
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Midnight2005Author Commented:
I don't know what you talking about, I don't find it if it's a funny joke.  Or maybe it's worse for Microsoft, it's totally random.

I went to About, it say Access 2003. (version 11) , Visual Basic version 6.3

I typed

Debug.Print Round(2.155, 2)
it's giving me 2.15...I don't know how you got 2.16.
Thanks

Note: I need Banker's Rounding, that's why I am not adding anything small.
Thanks for your help
0
 
GRayLCommented:
? round(2.15,1)
 2.2
? round(2.25,1)
 2.2
? round(2.35,1)
 2.4
? round(2.45,1)
 2.4

or

? round(2.435,2)
 2.44
? round(2.445,2)
 2.44
? round(2.455,2)
 2.46
? round(2.465,2)
 2.46
 
I'm runn A2K with SP3
0
 
Midnight2005Author Commented:
This is what I have:

? Round(2.155, 2)
 2.15
0
 
gvlobCommented:
GRayL, what version of Access are you using. I tried ? round(2.155,2) and got the same thing as Midnight2005, 2.15. According to Banker's rounding, it should round to 2.16.

LPurvis, what did you mean by "...the ureliability of that from region to region."?
0
 
Midnight2005Author Commented:
From what he said I undertood GrayL is Usind Access 2000 (A2K)
0
 
gvlobCommented:
GRayL, I just tried all your examples and got the same as you did, but for ? round(2.155,2) I get 2.15. What's the deal here?
0
 
gvlobCommented:
It seems that it's looking at the tenths place for rounding. I just tried ? round(2.345) and got 2.35 not 2.34 like we would expect in Banker's Rounding.
? round(2.445) gives you 2.44 like we expect. So, this leads me to believe that Access 2k3 looks at the tenths place instead of the next greater significant value.
0
 
Leigh PurvisDatabase DeveloperCommented:
I get 2.15 too - but if Ray says he's getting 2.16 I believe him.
So it isn't consistent.  Hence my suggested function.

It behaves the same in A2K and A2K3 for me.

FWIW Bankers rounding does apply to higher DP - hence why the function is able to be so simple.
0
 
gvlobCommented:
I agree with you LPurvis, I would go with a function. On the other hand, if GRayL is getting 2.16 there has to be a reason why.
0
 
Leigh PurvisDatabase DeveloperCommented:
There are many things I'd love to know about VBA and Access under the covers of which I've only able to deduce relatively small snippets over the years.
However MS has yet to call to enlighten me - so I write workarounds :-D
0
 
GRayLCommented:
Using 2.1n5 it rounded down on all n from 0 to 9.  When I changed either the 2 or .1 to some other digit and then varied n, it did the banker's round properly.  It looks like 2.1n5 is an anomaly.  Somebody should tell Bill;-)
0
 
Leigh PurvisDatabase DeveloperCommented:
I'll have a word when I'm next over for drinks.  :-)
0
 
GRayLCommented:
To be truthful, I tested numbers other than 2.155 and just assumed 2.155 would behave normally.  For the record, 2.155 rounds down on my machine to 2.15.  
0
 
GRayLCommented:
I guess I have to Go To Jail - huh?
0
 
metapherCommented:
I encountered this problem too in the past. And switched to the next version of Access. With the versions XP and following the Round-Issue wasn't a problem any more. My old comment:

The original Function Round() in VBA is not working correct, because it rounds always towards the next integer dividable by 2!!! There 1.5 and 2.5 will be rounded to 2!!! What a mess.

So it is not random where it rounds to. Always the even numbers are preffered. If you are not able to switch to a never Version of ACCESS you can help yourself in very creative ways: i.E. to a string and the back. I never tried, but changed my version..

Good luck!
0
 
Midnight2005Author Commented:
I am using the latest version 2003 :)
0
 
gvlobCommented:
GRayL, you go Directly to Jail, do not pass Go, do not collect $200 (in this case 500 points) very big :-)
0
 
Midnight2005Author Commented:
What do I do guys?
Have I been the only guy in planet who needed a simple Banker's rounding?
0
 
gvlobConnect With a Mentor Commented:
You could use your own custom function like LPurvis suggested, but it seems to be consistent on the tenths place. This should give you the same average as it would if it would work in the next significant figure.
0
 
Midnight2005Author Commented:
You're right, I am sure I can figure out something. It was fun. Thanks guys
0
 
Leigh PurvisDatabase DeveloperCommented:
If you save the function to a standard module - then you can use fRound *everywhere* that you would have used Round.
0
 
GRayLCommented:
Leigh, can you 'explain' what your function does - especially the contribution of Val()?
0
 
gvlobCommented:
I don't think you need Val() in the function.
0
 
Leigh PurvisDatabase DeveloperCommented:
Hi Ray - good to see they've let you out on Day Release! :-)
Seemed harsh to me in the first place.

For any interested parties...

First off Val is to make sure there's no ambiguity about data types.  We're dealing with a doube (or single - semantics ;-)
VBA can make guesses and we don't want that.
If you try it without that there you'll still get the inconsistent result.

Ultimately the core of the function raises the number of the index of 10 required to get to 1 dp (based on the number of dp's we want to end up with).
Then let's VBA perform a standard round on no dp's and divides the result by that power of 10 again to get back to where we were (and with the correct no of DP's).

That's all a pretty standard rounding technique.
0
 
GRayLCommented:
gvlob: I tested and it is necessary.  

Leigh: Thanks, as usual, an elegant solution.
0
 
Midnight2005Author Commented:
That's why I chosed the Excelent in rating. Thanks...
0
 
gvlobCommented:
You learn something everyday :-)
0
 
Leigh PurvisDatabase DeveloperCommented:
Cheers Ray.
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.

All Courses

From novice to tech pro — start learning today.