Hey there
Public Function SocialSecurity(GrossPay As Double) As
Double
Dim ss As Double
Dim rss As Double
ss = GrossPay * 0.062
rss = Round(ss, 2)
Called with GrossPay = 7.5
ss = .465
rss = .46 (but should be .47)
What's up??
Well, this is the nature of floating-point numbers in our modern
IT world. Since computers can only juggle with simple bits,
all data types (such as integers, floating-points etc.) have to be
somehow interpreted into sets of bits. In case of floating-
point numbers (Single and Double in VBA) according to
internationals standards (IEEE), this interpretation is not
an exact one, rounding errors occur. However, in Access
(and VBA), there is a workaround data type for this: Currency.
This is basically a long integer data type (64 bits) which is
divided by 10000 when displayed. This way, all the calculations
can be done without rounding errors (using integer arithmetic), but
the programmer is restricted to 4 usable right-of-comma positions...
In your case, rewrite the function using data type Currency:
Public Function SocialSecurity(GrossPay As Currency) As Currency
Dim ss As Currency
Dim rss As Currency
ss = GrossPay * 0.062
rss = myRound(ss, 2)
...
Note, that you have to supply your own round-function (using
data type currency) or else the round function will still disappoint you
by possibly rounding the result to an unexpected value:
Public Function myRound(v As Currency, pow As Integer) As Currency
myRound = Int(v * 10 ^ pow + 0.5) / 10 ^ pow
End Function
HTH,
Martin