开发者

Do something if one number is close to another number in VB6?

开发者 https://www.devze.com 2023-04-05 06:25 出处:网络
Say I have 2 numbers, 5550, and 5650, and I want to know if these two numbers are close, within 200 of each other开发者_如何学JAVA. How could I do this with VB6? I am clueless.Simply subtract the larg

Say I have 2 numbers, 5550, and 5650, and I want to know if these two numbers are close, within 200 of each other开发者_如何学JAVA. How could I do this with VB6? I am clueless.


Simply subtract the larger number (5650) from the smaller number (5550) and check if the result is less than your range (200). I use Abs so you don't need to check which number is greater.

Dim number1 As Integer = 5550
Dim number2 As Integer = 5650
Dim range As Integer = 200

If Abs(number1 - number2) <= range Then
    ' Here is where your numbers are within 200.
End If


If Abs(number1 - number2) < 200 Then
    'do something
EndIf

Warning: this will not handle integer overflow very nicely. If number1 is a very large negative number and number2 is a large positive number, this may produce strange results.

0

精彩评论

暂无评论...
验证码 换一张
取 消