I know you already have your answer, but here's something from a long time ago that I wrote:
Public Const FORCEROUNDUP = 1
Public Const FORCEROUNDDOWN = 2
Public Const NOFORCEROUND = 3
Public Function fRounding(NumberBeingRounded As Double, Optional NumOfPlaces As Integer, Optional RoundUpOrDown As Integer = 3) As Double
'RoundUpOrDown - Pass FORCEROUNDUP, FORCEROUNDDOWN, NOFORCEROUND or pass nothing.
'Randall Vollen
'National City Bank
'This was created, because it was brought to my attention by someone that
'there is supposedly 'no way' to round in access. I looked into it further and found that
'there is no 'easy' way to do it. This is a very simplistic piece of code that takes in
'any number with any number of decimal places and rounds to the decimal of your choice.
'Mind that you may be led to believie the following will round for you:
'-1 * fix(number * -100)/100 or
'-1 * cint(number * -100)/100
'Unfortunately due to the nature of the truncation of Access's VBA if you use an EVEN Number that happens to fall on
'a half (82.5) then it will always round down- otherwise that is a great one line train of thought.
'*** stuff to delete ***
'TEST DATA that was thrown in:
'Dim NumberBeingRounded As Double
'NumberBeingRounded = 83.55769921
'Dim NumOfPlaces As Integer
'NumOfPlaces = 7
'*** end of stuff to delete ***
Dim y As Long 'this holds whole part of the number or the part that will eventually be rounded
Dim z As Double 'this is the part that decideds if the number is rounded up or down
Dim Factor As Double 'this holds number 1 - 1,000,000 that decides to what place to round
Dim HalfPoint As Double
If RoundUpOrDown = NOFORCEROUND Then
'No Forcing at all
HalfPoint = 0.5
ElseIf RoundUpOrDown = FORCEROUNDUP Then
'Force rounding up
HalfPoint = 0
Else
'Force rounding down
HalfPoint = 1
End If
'if you don't specify the number of places, then it's rounded to the nearest whole number
If IsNull(NumOfPlaces) Then
NumOfPlaces = 0
End If
'If NumOfPlaces > 7 Then
'sets which decimal to round to
Factor = 10 ^ NumOfPlaces
'multiply number by factor, so we can round
NumberBeingRounded = NumberBeingRounded * Factor
'rounding
y = Fix(NumberBeingRounded)
z = NumberBeingRounded - y
NumberBeingRounded = Fix(NumberBeingRounded)
If z >= HalfPoint Then
NumberBeingRounded = NumberBeingRounded + 1
End If
'end of rounding
'return number back to it's original
NumberBeingRounded = NumberBeingRounded / Abs(Factor)
'return value
fRounding = NumberBeingRounded
End Function
Randall Vollen
National City Bank Corp.