Dex
0
Q:

vba mod function floating point number

'VBA does have the 'mod' operator, but bear in mind that it returns
'an integer value... always. And it converts both inupts to integers 
'before the calculation.
'
'In contrast, the 'MOD()' worksheet function returns a floating point
'value. 
'
'Also, VBA's 'mod' operator cannot hanlde negative values,
'always returning ZERO.
'
'The VBA function below:
'1.) Can return a decimal value
'2.) Can handle negative values
'3.) Uses a Decimal variant data subtype to reduce 
'    floating point error
'4.) The above three items make this function more accurate than
'    either the VBA 'mod' operator and the 'MOD()' worksheet function.

Function Mod2(n, divisor)
    Mod2 = CDec(n) - divisor * Int(n / divisor)
End Function
5

New to Communities?

Join the community