• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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
0
ISC
Asked:
ISC
  • 3
  • 3
  • 2
  • +4
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now