May be somebody can explain to me how Round function works in Access. This article claims VBA6 uses banker's rounding, which means that exact half rounds to the closest even digit. This article claims VBA6 uses banker's rounding ONLY when rounding to integer.
So how do I get Round(2.245, 2) = 2.24 while Round(1.245, 2) = 1.25 ?
I use both Acc2K and Acc2K2.
8 5107
May be somebody can explain to me how Round function works in Access. This article claims VBA6 uses banker's rounding, which means that exact half rounds to the closest even digit. This article claims VBA6 uses banker's rounding ONLY when rounding to integer.
So how do I get Round(2.245, 2) = 2.24 while Round(1.245, 2) = 1.25 ?
I use both Acc2K and Acc2K2.
Hi, dima.
As far as I've understood you, you mean rounding to lowest/uppest digit depending on parity of integer part.
If so, the function below does it. -
Public Function Round1(ByVal dblInput As Double, ByVal intDigits As Integer) As Double
-
-
Dim intParity As Integer
-
-
intParity = Int(dblInput) - Int(Int(dblInput) / 2) * 2
-
Round1 = Int(dblInput * 10 ^ intDigits + intParity / 2) / 10 ^ intDigits
-
-
End Function
-
Actually, I didn't read the second article as saying that VBA uses banker's rounding ONLY when rounding to integer. What I read was that if the integer portion of the number was even Access would round down, which is to say the same thing the first article said, that Access always rounds to the closest even integer.
Here's a function that I think will do what you want. Place it in a standard module. If you don't have a standard module to place it in, create one for it. Just remember do not name the module the same name as the function! This confuses Access! - Public Function RoundTotal(ByVal dblNumber As Double, ByVal intDecimals As Integer) As Double
-
-
' : 0.5 is rounded up
-
'Parameters : dblNumber - number to round
-
' : intDecimals - number of demal places
-
' to round to
-
-
' : (positive for right of decimal, negative for left
-
'Returns : Rounded number
-
-
Dim dblFactor As Double
-
Dim dblTemp As Double ' Temp var to prevent rounding problems in INT()
-
-
dblFactor = 10 ^ intDecimals
-
dblTemp = dblNumber * dblFactor + 0.5
-
RoundTotal = Int("" & dblTemp) / dblFactor
-
-
End Function
The, in code, call it just like Round() except use
RoundTotal( YourNumberToRound, NumberOfDigits)
Linq ;0)>
Edit: Sorry, Fish, got distracted mid-post! ;0)>
Nice hint, Link.
I mean this - converting number to string before passing to Int function.
I was near trying to solve the problem with Format function, but your solution is really nice. -
Public Function Round1(ByVal dblInput As Variant, ByVal intDigits As Integer) As Double
-
-
Dim intParity As Integer
-
-
intParity = Int(dblInput) - Int(Int(dblInput) / 2) * 2
-
Round1 = Int(Str(dblInput * 10 ^ intDigits + intParity / 2)) / 10 ^ intDigits
-
-
End Function
-
Linq, FishVal - thanks for the tips, but I think you've got me wrong. What I am looking for is to understand how an existing Round function works, cause it seems that it works differently than Banker's round (see examples in my first post).
I was unaware of that weird behavior before, so now if I change the standard Round to something else, this may cause existing invoices to change :)
Linq, FishVal - thanks for the tips, but I think you've got me wrong. What I am looking for is to understand how an existing Round function works, cause it seems that it works differently than Banker's round (see examples in my first post).
I was unaware of that weird behavior before, so now if I change the standard Round to something else, this may cause existing invoices to change :)
It does Banker's rounding - rounds to the nearest even digit
Round(2.245, 2) = 2.24
Round(2.255, 2) = 2.26
From your example
So how do I get Round(2.245, 2) = 2.24 while Round(1.245, 2) = 1.25 ?
I understood that you expect it to round "half" to lower digit if integer part is even, and to upper digit if it is odd. Am I right?
To the best of my knowledge Round function doesn't work so, as well as any other standard rounding function. :)
It does Banker's rounding - rounds to the nearest even digit
Round(2.245, 2) = 2.24
Round(2.255, 2) = 2.26
From your example
I understood that you expect it to round "half" to lower digit if integer part is even, and to upper digit if it is odd. Am I right?
NO. Nothing to do with Integer part parity.
Following the nearest even digit algorithm, I expect Round(1.245, 2) = 1.24 (4 is the nearest even digit) - am I right ?
But what I really get is 1.25 !
NO. Nothing to do with Integer part parity.
Following the nearest even digit algorithm, I expect Round(1.245, 2) = 1.24 (4 is the nearest even digit) - am I right ?
But what I really get is 1.25 !
Weird.
Really when NumDigitsAfterDecimal>0, rounding direction is somewhat unpredictable. I suppose that this caused by VBA math precision.
As for me I would prefer to use smthng like this to get result of Banker's rounding. -
Public Function Round2(ByVal dblInput As Variant, ByVal intDigits As Integer) As Double
-
Round2 = Round(Str(dblInput * 10 ^ intDigits), 0) / 10 ^ intDigits
-
End Function
-
Amaizing !
When data type is Currency or Decimal, Round works as it supposed to (Banker's round).
But when the data type is double, Round result is something undefined !
For example:
Round(CCur(1.245) ,2) = 1.24 (as expected)
Round(CDbl(1.245) ,2) = Round(1.245 ,2) = 1.25 ! Wrong !
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Matias Silva |
last post by:
Can anybody tell me why I am getting rounding errors
using the ROUND function.
3.7125 rounds to 3.70 when I use the following:
TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2)))
The correct...
|
by: Penguin |
last post by:
At some long ago time Steve Jorgensen answered thus:
Subject: Re: How can I round a time?
Newsgroups: comp.databases.ms-access
Date: 1998/12/11
Access represents a date internally as a double...
|
by: m |
last post by:
all,
i am trying to use the function round() which I found through google
to be declared in math.h (
http://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html).
this function does...
|
by: Ronald W. Roberts |
last post by:
I'm having a problem understanding the Round function. Below are quotes
from two
books on VB.NET. The first book shows examples with one argument and
how it
rounds. The second book something...
|
by: Karl O. Pinc |
last post by:
FYI,
It'd be nice if the error message from a REFERENCES
constraint mentioned the column name into which
the bad data was attempted to be inserted.
In PostgreSQL 7.3:
sandbox=> insert into...
|
by: Chris Davoli |
last post by:
The folllowing will round to 526, but it should round to 527. It works
correctly for all other numbers, except for this one. Does anybody know of a
bug in Math.Round?
Dim ldecWater As Decimal =...
|
by: Phat G5 (G3) |
last post by:
Has anyone found a reliable way to force JS to round to a specific number of
places? Every time I try I get different results. For example, I'd need to
round 3.4589 to 2 places. What is the most...
|
by: thaisummitneel |
last post by:
sir
I have created a database in ms-access.Connected database using ADODB
I have written a query to round integer to nearest value such as
con.execute"update tablename set...
|
by: josh logan |
last post by:
Hello,
I need a round function that _always_ rounds to the higher integer if
the argument is equidistant between two integers. In Python 3.0, this
is not the advertised behavior of the built-in...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |