Article ID: 155696
Article Last Modified on 1/19/2007
Parameter Value --------- --------------------------------------------------------- Amt The numeric value you want to round RoundAmt The increment to which Amt will be rounded Direction Constant indicating which direction to round (up or down)For example, RoundToNearest(3.33, 0.1, vb_roundup) returns the value 3.4.
Option Explicit
Public Const vb_roundup = 1
Public Const vb_rounddown = 0
NOTE: In versions 1.x and 2.0, use the word "Global" instead of
"Public."
Function RoundToNearest (Amt As Double, RoundAmt As Variant, _
Direction As Integer) As Double
On Error Resume Next
Dim Temp As Double
Temp = Amt / RoundAmt
If Int(Temp) = Temp Then
RoundToNearest = Amt
Else
If Direction = vb_rounddown Then
Temp = Int(Temp)
Else
Temp = Int(Temp) + 1
End If
RoundToNearest = Temp * RoundAmt
End If
End Function
?RoundToNearest(1.36, 0.25, vb_roundup)
?RoundToNearest(1.36, 0.05, vb_rounddown)
?RoundToNearest(1.36, 0.75, vb_roundup)
Additional query words: factor cents dollars quarter dime nickel roundup rounddown round-up round-down
Keywords: kbhowto kbprogramming KB155696