trangdv.pchg > 29-12-16, 01:05 PM
ongke0711 > 29-12-16, 02:22 PM
Public Function Round( _
ByVal Number As Variant, NumDigits As Long, _
Optional UseBankersRounding As Boolean = False) As Double
' ---------------------------------------------------
' From "Visual Basic Language Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 2000; Sybex, Inc. All rights reserved.
' ---------------------------------------------------'
Dim dblPower As Double
Dim varTemp As Variant
Dim intSgn As Integer
If Not IsNumeric(Number) Then
' Raise an error indicating that'
' you have 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.'
Round = intSgn * Int(varTemp) / dblPower
End Function
trangdv.pchg > 29-12-16, 03:25 PM
ongke0711 > 29-12-16, 03:44 PM
trangdv.pchg > 29-12-16, 09:07 PM
ongke0711 > 29-12-16, 10:28 PM
trangdv.pchg > 30-12-16, 08:23 PM
ongke0711 > 30-12-16, 09:50 PM
trangdv.pchg > 01-01-17, 01:06 PM
ongke0711 > 01-01-17, 01:51 PM