We help IT Professionals succeed at work.

VB6 Function For Working Out The Difference Between Numbers

ISC
ISC asked
on
Medium Priority
399 Views
Last Modified: 2008-02-01

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
Comment
Watch Question

Commented:
Public Function subtract(a As Integer, b As Integer) As Integer
    subtract = a - b
End Function
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
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).

Commented:
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

Commented:
buildin VB6 function? no
ISC

Author

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

Commented:
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

ISC

Author

Commented:


If I do....

-150 + -50 then i get -200

I want +100

-50 is +100 greater then -150

Ian

Commented:
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

Commented:
EDDYT's modification will work.
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

Commented:

-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



ISC

Author

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.