Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Round Function in Access 97

Status
Not open for further replies.

Geraldo

Programmer
Mar 18, 2002
37
PT
how can i round a value in access 97, in access 2000 there is a function round.
any help
 
I wrote my own...
Rounds the passed number to required decimal places..

eg dblRound(4.556, 2) returns 4.56
dblRound(4.55, 1) returns 4.5

Hope it's some help...

Function dblRound(ByVal vdblNumber As Double, ByVal vintDecimalPlaces As Integer) As Double
Dim dblWholePart As Double
Dim dblRemainder As Double
Dim dblDecimalMultiplier As Double
On Error GoTo ErrorHandler

dblDecimalMultiplier = (10 ^ vintDecimalPlaces)
vdblNumber = vdblNumber * dblDecimalMultiplier

dblWholePart = Int(vdblNumber)
dblRemainder = vdblNumber - dblWholePart
If dblRemainder >= 0.6 Then
dblRound = (dblWholePart + 1) / dblDecimalMultiplier
Else
dblRound = dblWholePart / dblDecimalMultiplier
End If

Exit Function

ErrorHandler:

MsgBox "Error in 'Round'." & vbCrLf & Err.Description, , "Error"

End Function

 
Here's one that works well - it's widely posted on the web.
Public Function BankersRounding(ByVal Number As Variant, NumDigits As Long, Optional UseBankersRounding As Boolean = False) As Double
Dim dblPower As Double
Dim varTemp As Variant
Dim intSgn As Integer

If Not IsNumeric(Number) Then
' Raise an error indicating that
' you've supplied an invalid parameter.
Err.Raise 5
End If

dblPower = 10 ^ NumDigits
' Is this a negative number, or not?
' intSgn will contain -1, 0, or 1.
intSgn = Sgn(Number)
Number = Abs(Number)

'Do the major calculation.
varTemp = CDec(Number) * dblPower + 0.5

'Now round to nearest even, if necessary.
If UseBankersRounding Then
If Int(varTemp) = varTemp Then
' You could also use:
' varTemp = varTemp + (varTemp Mod 2 = 1)
' instead of the next If ...Then statement,
' but I hate counting on TRue == -1 in code.
If varTemp Mod 2 = 1 Then
varTemp = varTemp - 1
End If
End If
End If
'Finish the calculation.
BankersRounding = intSgn * Int(varTemp) / dblPower

End Function

'Example of usage would be.
'cTranAmt = BankersRounding(cTranAmt, 2)
'which would round the value to two decimal places
 
Function DnRound(Number As Double, Decimals As Integer) As Double
DnRound = Int(Number * 10 ^ decimals + 0.5) / 10 ^ decimals
End Function

HTH

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top