Get Remainder-VBForums

1. 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 dNumber2Dim dDiv As DoubleDim iInt As IntegerDim 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?

2. Re: Get Remainder

Modulus division.

Code:
` VariableName = 3 Mod B`

Something like that.

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

4. 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:
`iInt = CInt(dDiv)`
That did give the "3", but the result was same very small value, but not zero.

5. 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 dNumber2Dim dDiv As DoubleDim dResult As DoubleDim 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`

6. 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
•

Featured