Round to Nearest Integer

K

Kou Vang

How do I round ([8067]+[350])/2 = 4208.5 to 4209? I have tried several
things mentioned, and have yet to find one that works. And from what I can
tell, the Round function does not work!
 
A

Allen Browne

The Round() function in Access rounds towards the nearest even number, so
8.5 is rounded down while 7.5 is rounded up.

If you want a function that always round .5 downwards, you could use a
function one such as this:
http://www.mvps.org/access/modules/mdl0054.htm

Be sure to rename it so it does not fight with the built-in one.
 
D

Dale Fye

The round function works just as designed, unfortunately, it does not work
the way that some would like. Basically, the round function works perfectly
as long as the last number to the right of the decimal point is not a 5. If
it is a 5, the function looks at the previous number to determine whether to
round up or down. If the previous number is odd, it rounds up, if even, it
rounds down.

This prevents large errors from occuring when rounded numbers are added. To
do what you want(round up in all instances where the value is 5, you could
write a function to test to determine whether the last digit is a five, and
if so, add some insignificantly small value to it.

Public Function MyRound(ByVal dblValue As Double, _
Optional DecPlaces As Integer = 0) As Double

Dim myString As String
myString = CStr(dblValue)
If Right(myString, 1) = 5 Then
dblValue = dblValue + 0.00001
End If
MyRound = Round(dblValue, DecPlaces)

End Function

HTH
Dale
 
M

Michel Walsh

Hi,

is x > 0 then



int( 0.5 + x )



Note that even if x appears to be .5, it can be .499998 or .5000001. So,
unless x is a decimal number (CDec), you can still get unexpected result due
to approximation error of double / float numbers. As explained by Dale Fye,
Round uses the banking rounding, while int just take the integer-part. If x
is < .5, adding .5 to it does leave int(0.5 + x ) = int(x); while if x>.5,
then int(0.5+x) = 1+ int(x).



Hoping it may help
Vanderghast, Access MVP
 
D

Dale Fye

Michel,

In the Army, we call that Artillery Rounding!

Dale

Michel Walsh said:
Hi,

is x > 0 then



int( 0.5 + x )



Note that even if x appears to be .5, it can be .499998 or .5000001. So,
unless x is a decimal number (CDec), you can still get unexpected result
due to approximation error of double / float numbers. As explained by Dale
Fye, Round uses the banking rounding, while int just take the
integer-part. If x is < .5, adding .5 to it does leave int(0.5 + x ) =
int(x); while if x>.5, then int(0.5+x) = 1+ int(x).



Hoping it may help
Vanderghast, Access MVP



Kou Vang said:
How do I round ([8067]+[350])/2 = 4208.5 to 4209? I have tried several
things mentioned, and have yet to find one that works. And from what I
can
tell, the Round function does not work!
 
Top