
May 11th, 2005, 12:16 AM
#1
Thread Starter
Frenzied Member
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:
Public Function GetRemainder(dNumber1 As Double, dNumber2 As Double) As Double 'Returns the remainder after diving dNumber1 by dNumber2 Dim dDiv As Double Dim iInt As Integer Dim dResult As Double dDiv = dNumber1 / dNumber2 iInt = Int(dDiv) dResult = dDiv  iInt GetRemainder = dResult * dNumber2 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?

May 11th, 2005, 05:11 AM
#2
Frenzied Member
Re: Get Remainder
Modulus division.
Code:
VariableName = 3 Mod B
Something like that.

May 11th, 2005, 12:15 PM
#3
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:
Private Function Mod(intFirstNumber As Integer, intSecondNumber As Integer) As Integer If intSecondNumber = 0 Then Mod = 0 Exit Function End If Mod = intFirstNumber  (Int(intFirstNumber / intSecondNumber) * intSecondNumber) End Function
Cheers,
RyanJ

May 11th, 2005, 04:01 PM
#4

May 11th, 2005, 08:27 PM
#5
Thread Starter
Frenzied Member
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:
Public Function GetRemainder(dNumber1 As Double, dNumber2 As Double) As Double 'Returns the remainder after diving dNumber1 by dNumber2 Dim dDiv As Double Dim dResult As Double Dim sDiv As String dDiv = dNumber1 / dNumber2 sDiv = CStr(dDiv) If InStr(sDiv, ".") = 0 Then dResult = dNumber1  (CInt(dDiv) * dNumber2) Else dResult = dNumber1  (Int(dDiv) * dNumber2) End If dResult = Format(dResult, "####0.00") GetRemainder = dResult End Function

Feb 5th, 2013, 07:53 AM
#6
Junior Member
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 roundeddown 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: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

Forum Rules

Click Here to Expand Forum to Full Width
Survey posted by VBForums.
