|
| 1 | +Attribute VB_Name = "udf_modulus_zero" |
| 2 | +Option Explicit |
| 3 | + |
| 4 | +Private Sub ModulusZero_Test() |
| 5 | + Debug.Print "ModulusZero(3.001, 3) -> " & ModulusZero(3.001, 3) |
| 6 | + Debug.Print "ModulusZero(2, 0.3) -> " & ModulusZero(2, 0.3) |
| 7 | + Debug.Print "ModulusZero(3, 0.3) -> " & ModulusZero(3, 0.3) |
| 8 | + Debug.Print "ModulusZero(6.3, 0.3) -> " & ModulusZero(6.3, 0.3) |
| 9 | + Debug.Print "ModulusZero(57, 0.4) -> " & ModulusZero(57, 0.4) |
| 10 | + Debug.Print "ModulusZero(6324692, 415) -> " & ModulusZero(6324692, 415) |
| 11 | + Debug.Print "ModulusZero(6324600, 415) -> " & ModulusZero(6324600, 415) |
| 12 | + Debug.Print "ModulusZero(6324600.99, 415) -> " & ModulusZero(6324600.99, 415) |
| 13 | +End Sub |
| 14 | + |
| 15 | +Public Function ModulusZero(ByVal dblInputValue As Single, ByVal dblDivisor As Single) As Boolean |
| 16 | + 'This procedure checks to see if any integer or decimal is equally divisible by the |
| 17 | + 'provided divisor. Note that the precision of the input value is evaluated at the same level |
| 18 | + 'as the precision of the divisor. |
| 19 | + If dblInputValue = dblDivisor Then |
| 20 | + 'If the numbers are the same, then they are equally divisble. |
| 21 | + ModulusZero = True |
| 22 | + ElseIf dblInputValue < dblDivisor Then |
| 23 | + 'If the input value is smaller than the divisor, then it cannot be equally divisible. |
| 24 | + ModulusZero = False |
| 25 | + Else |
| 26 | + 'Microsoft warns about comparisons of doubles and singles and that they may not always |
| 27 | + 'evaluate how you exptect them to. Therefore, I treat their results of the maths as |
| 28 | + 'strings. |
| 29 | + If InStr(CStr(dblInputValue), ".") = 0 And InStr(CStr(dblDivisor), ".") = 0 Then |
| 30 | + ModulusZero = IIf(InStr(CStr(CLng(dblInputValue) / CLng(dblDivisor)), ".") > 0, False, True) |
| 31 | + Else |
| 32 | + ModulusZero = IIf(InStr(dblInputValue / dblDivisor, ".") > 0, False, True) |
| 33 | + End If |
| 34 | + End If |
| 35 | +End Function |
| 36 | + |
| 37 | + |
0 commit comments