Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Rounding up number in Access 97 ?
Message
 
To
18/07/2000 10:29:29
General information
Forum:
Visual Basic
Category:
VBA
Miscellaneous
Thread ID:
00393795
Message ID:
00394800
Views:
12
See---> http://www.mvps.org/access/modules/mdl0054.htm

' From "Visual Basic Language Developer's Handbook"
As evident by the numerous threads on this topic in the different newsgroups, a lot of people have come across rounding errors in VBA when working with floating-point numbers. The primary reason behind such errors is that floating-point calculations with Single or Double data type often can't be represented in binary (due to large range of numbers these data types can operate on), leading to rounding inaccuracies.

The workaround is to work with scaled (Fixed point) numbers, that is, Currency and Decimal data types. Here's a function that always works (using "banker's rounding") by converting the value passed to a Decimal data type.

' by Ken Getz and Mike Gilbert
' Copyright 2000; Sybex, Inc. All rights reserved.
' ---------------------------------------------------
'
' ********** Code Start **************
Public Function Round( _
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.

Hope this brings some relief...

Russ

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
' ********** Code End **************
Previous
Reply
Map
View

Click here to load this message in the networking platform