Rounding vlaues

BoroLee

Registered User.
Local time
Today, 11:57
Joined
Aug 30, 2000
Messages
90
Can anyone tell me a piece of code which will allow me to round values up or down.

Thanks,
Lee
 
I use this code and it works great!
Function RoundToNearest(dblNumber As Currency, varRoundAmount As Double, Optional varUp As Variant) As Double
Dim dblTemp As Double
Dim intTemp As Integer

dblTemp = dblNumber / varRoundAmount
intTemp = Int(dblTemp)

If intTemp = dblTemp Then
RoundToNearest = dblNumber
Else
If IsMissing(varUp) Then
'round down
dblTemp = intTemp

Else
'round up
dblTemp = intTemp + 1
End If
RoundToNearest = dblTemp * varRoundAmount
End If
End Function
 
I use this, which will round to any number of decimal places:

Code:
Public Function MyRound(YourNumber As Double, Places As Integer) As Double
MyRound = YourNumber * (10 ^ Places)
MyRound = MyRound + 0.5
MyRound = Int(MyRound)
MyRound = MyRound / (10 ^ Places)
End Function

You might be asking why in stages like that, instead of doing it using a single expression, but if you do, because of the way that Access stores floating point numbers internally, it will occasionally round the wrong way, doing the operation in stages overcomes this.

Mike
 

Users who are viewing this thread

Back
Top Bottom