VB6 Function For Working Out The Difference Between Numbers


Is there a built in VB6 function the allows you to pass in 2 numbers (these numbers can be either negative or postive) and get back the difference which could be a negative or positive difference..??

Thanks Ian
LVL 1
ISCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hongjunCommented:
Public Function subtract(a As Integer, b As Integer) As Integer
    subtract = a - b
End Function
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Please define 'the difference which could be a negative or positive difference'.

Otherwise, a simple subtraction as hongjun posted will work.

You can also get the absolute value of a number if necessary using abs(YourNumber).
0
diasfCommented:
Hi ISC,

I suppose I don't really understand what you are trying to do. Isn't that exactly a difference?

Like

A=10
B=-15
C=A-B=10-(-15)=10+15=25

Is this what you want?

diasf
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

EDDYKTCommented:
buildin VB6 function? no
0
ISCAuthor Commented:
Examples...

Example 1:    -30 and  50   = a positive increase of +20

Example 2:    50 and  -60   = a negative decrease of -10

Example 3:    100 and 150   = a positive increase of +50

Example 4:    -50 and  -20   = a positive increase of +30

Example 5:    -150 and  -200   = a negative decrease of -50

Ian
0
hongjunCommented:
Try this

Private Sub Form_Load()
    Dim intExample As Integer
   
    intExample = diff(-30, 50)
    MsgBox intExample
End Sub

Public Function diff(a As Integer, b As Integer) As Integer
    diff = a + b
End Function

0
ISCAuthor Commented:


If I do....

-150 + -50 then i get -200

I want +100

-50 is +100 greater then -150

Ian
0
EDDYKTCommented:
Example 1:    -30 and  50   = a positive increase of +20  'should this be +80

Example 2:    50 and  -60   = a negative decrease of -10 ' hsould this be -110

Example 3:    100 and 150   = a positive increase of +50

Example 4:    -50 and  -20   = a positive increase of +30

Example 5:    -150 and  -200   = a negative decrease of -50

if yes then

Private Sub command1_Click()
Debug.Print diff(-30, 50)

Debug.Print diff(50, -60)

Debug.Print diff(100, 150)

Debug.Print diff(-50, -20)

Debug.Print diff(-150, -200)


End Sub
Public Function diff(a As Integer, b As Integer) As Integer
    diff = b - a
End Function

0
hongjunCommented:
EDDYT's modification will work.
0
AtanAsfalothCommented:
Example 1:    -30 and  50   = a positive increase of +20

Example 2:    50 and  -60   = a negative decrease of -10


Unless EDDYKT is right and you do mean an increase of +80 and a decrease of -110 are you trying to compare the absolute values and after that the change of sign?

Like this?


Public Function diff(a As Integer, b As Integer) As Integer
    'Declare Vars
    Dim intDiff As Integer
    '
    intDiff = Abs(b) - Abs(a)
    If a + b > 0 Then
        diff = intDiff
    ElseIf a + b < 0 Then
        diff = -intDiff
    ElseIf a + b = 0 Then
        diff = b - a
    End If
End Function


This function gives your desired output

Good luck

Atan Asfaloth
0
mikemCommented:

-150 + -50 then i get -200

That is the correct answer

If you are looking to work out difference, then you always need to subtract ( -150) - (-50) = -100

Mike



0
ISCAuthor Commented:
AtanAsfaloth ,

Yes this is what I am trying to do....

"you trying to compare the absolute values and after that the change of sign?"

Basically work out the difference then adjust the sign if nessecary....

Thanks Ian

0
AtanAsfalothCommented:
Ian,

The function I wrote returns the output you want and have stated in your examples. I was, however, not sure what you wanted with the input diff(-50, 50). Currently this outputs 100, whereas diff(50, -50) outputs -100. I think that you require the function to output 0 in this case (the absolute values are the same and all...) so I rewrote the function. This will output the values stated in your example, and for example diff(-50, 50) will output 0.

I hope this helps :)


'Code

Public Function diff(a As Integer, b As Integer) As Integer
    'Declare Vars
    Dim intDiff As Integer
    '
    intDiff = Abs(b) - Abs(a)
    If a + b >= 0 Then
        diff = intDiff
    ElseIf a + b < 0 Then
        diff = -intDiff
    End If
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.