Results 1 to 6 of 6

Thread: Get Remainder

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,373

    Get Remainder

    This should be so simple. I need a function that when passed 2 doubles and the second is divided into the first, returns the remainder (including decimal places).

    I have written this. It works most of the time:

    VB Code:
    1. Public Function GetRemainder(dNumber1 As Double, dNumber2 As Double) As Double
    2. 'Returns the remainder after diving dNumber1 by dNumber2
    3. Dim dDiv As Double
    4. Dim iInt As Integer
    5. Dim dResult As Double
    6.  
    7. dDiv = dNumber1 / dNumber2
    8.  
    9. iInt = Int(dDiv)
    10.  
    11. dResult = dDiv - iInt
    12.  
    13. GetRemainder = dResult * dNumber2
    14.  
    15. End Function

    However, when I passed dNumber1 = 1.2 and dNumber2 = 0.4, I would expect it to return 0 but it returns 0.4 because

    Int(1.2 / 0.4) returns 2 not 3. Why??

    Is there an intrinsic VB function that does this operation or does someone have a function that does work?

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Get Remainder

    Modulus division.


    Code:
     VariableName = 3 Mod B

    Something like that.

  3. #3
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Get Remainder

    Well, if you are looking for a function that will do it without the built in VB one then this will do it

    VB Code:
    1. Private Function Mod(intFirstNumber As Integer, intSecondNumber As Integer) As Integer
    2.     If intSecondNumber = 0 Then
    3.         Mod = 0
    4.         Exit Function
    5.     End If
    6.     Mod = intFirstNumber - (Int(intFirstNumber / intSecondNumber) * intSecondNumber)
    7. End Function

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Get Remainder

    Strange

    Since you want to use this Function on doubles the previous posts w'ont help.
    To work around that problem, check if the second double is not an integer (i. e. has digits behind the decimal), if yes multiply both numbers in order that the second is an integer and at the end divide the result by the same factor.

    But why is it that way????

    I even tried to do it with this line:
    VB Code:
    1. iInt = CInt(dDiv)
    That did give the "3", but the result was same very small value, but not zero.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,373

    Re: Get Remainder

    The Int function is supposed to round DOWN to the closest integer.

    eg. Int(3.8) = 3
    Int(21.2) = 21

    whereas CInt rounds UP, so CInt(3.8) = 4

    so Int(1.2 / 0.4) should return Int(3) which is 3.

    The procedure needs to round DOWN to get the correct result so I need to use the Int function.

    I modified the procedure as follows converting dDiv to a string and searching the string for a decimal point and using Int if it finds one and CInt if it doesn't.

    Seems to give me the correct result.

    VB Code:
    1. Public Function GetRemainder(dNumber1 As Double, dNumber2 As Double) As Double
    2. 'Returns the remainder after diving dNumber1 by dNumber2
    3. Dim dDiv As Double
    4. Dim dResult As Double
    5. Dim sDiv As String
    6.  
    7. dDiv = dNumber1 / dNumber2
    8.  
    9. sDiv = CStr(dDiv)
    10.  
    11. If InStr(sDiv, ".") = 0 Then
    12.     dResult = dNumber1 - (CInt(dDiv) * dNumber2)
    13. Else
    14.     dResult = dNumber1 - (Int(dDiv) * dNumber2)
    15. End If
    16.  
    17. dResult = Format(dResult, "####0.00")
    18.  
    19. GetRemainder = dResult
    20.  
    21. End Function

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Resolved Re: Get Remainder

    I realize this is old, but for the person out there that is googling for the answer... this is what I used for the process:

    Solution:

    Code:
    'Returns the remainder of the division: num1 / num2.
    Public Function Remainder(num1 As Double, num2 As Double) As Double
        If (num1 / num2) = Int(num1 / num2) Then 'Check to see if the result is an integer by comparing the original result to a rounded-down result.
            'Since the results are equal, the result is a whole number
            Remainder = 0 'Return 0 since there is no remainder.
        Else
            Dim tmp As Double
            tmp = num1 - Int(num1 / num2) * num2 'Remove the integer portion of the number by subtracting it from num1.
            Remainder = tmp / num2 'Return the remainder portion.
        End If
    End Function
    Note, I want to make this a tad bit more clear by explaining this:
    Code:
    Int(num1 / num2)
    This value equates to an integer that indiciates how many times num2 can be taken out of num1.

    So, when we use this to find the remainder, we were actually calculating the numerator of our return value.
    The following subtracts the evenly divisible portion of the division from num1.
    Code:
    tmp = num1 - Int(num1 / num2) * num2
    Thus, that is how we are left with a decimal remainder!

    God Bless!
    -Nick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width