subtract = a - b

End Function

Solved

Posted on 2006-03-28

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

13 Comments

Otherwise, a simple subtraction as hongjun posted will work.

You can also get the absolute value of a number if necessary using abs(YourNumber).

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

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

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

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

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

-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

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

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

By clicking you are agreeing to Experts Exchange's Terms of Use.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**14** Experts available now in Live!