Jump to content
Sign in to follow this  
TalivanIBM

[SOLVED] Error comparing Digits

Recommended Posts

TalivanIBM

i compare digits in a simple gui and i get a bad comparison

#include <GuiConstants.au3>
   
   Local $dig1 = "No", $dig2 = "No", $int1 = "No", $int2 = "No"
   
   GUICreate("TEST", 150, 150)
   $1 = GUICtrlCreateInput("1000", 5, 5, 120)
   $2 = GUICtrlCreateInput("500", 5, 40, 120)
   $BUTTON = GUICtrlCreateButton("COMPARE", 5, 80, 60, 25)
   GUISetState()
   While 1
       $msg = GUIGetMsg()
       Select
           Case $msg = $GUI_EVENT_CLOSE
               Exit
           Case $msg = $BUTTON
               $n1 = GUICtrlRead($1)
               If StringIsDigit($n1) Then $dig1 = "Yes"
               If StringIsInt($n1) Then $int1 = "Yes"
               $len1 = StringLen($n1)
               $n2 = GUICtrlRead($2)
               If StringIsDigit($n2) Then $dig2 = "Yes"
               If StringIsInt($n2) Then $int2 = "Yes"
               $len2 = StringLen($n2)
               If $n1 < $n2 Then
                   $compare = "Less Than"
               ElseIf $n1 > $n2 Then
                   $compare = "Greater Than"
               ElseIf $n1 = $n2 Then
                   $compare = "Equal"
               EndIf
               MsgBox(0, $compare, "Input 1 is " & $compare & " input 2" & @LF & "Input 1 is digit: " & $dig1 & @TAB & "Is Integer: " _
               & $int1 & @TAB & "Length: " & $len1 & @LF & "Input 2 is digit: " & $dig2 & @TAB & "Is Integer: " & $int2 & @TAB & "Length: " & $len2)
               If StringCompare($n1, $n2) < 0 Then
                   $compare = "Less Than"
               ElseIf StringCompare($n1, $n2) > 0 Then
                   $compare = "Greater Than"
               ElseIf StringCompare($n1, $n2) = 0 Then
                   $compare = "Equal"
               EndIf
               MsgBox(0, $compare, "Input 1 is " & $compare & " input 2" & @LF & "Input 1 is digit: " & $dig1 & @TAB & "Is Integer: " _
               & $int1 & @TAB & "Length: " & $len1 & @LF & "Input 2 is digit: " & $dig2 & @TAB & "Is Integer: " & $int2 & @TAB & "Length: " & $len2)
       EndSelect
   WEnd

if i compare 1000 with 500 like i put in this example, 1000 is less than 500 (in direct comparison ex: 1000 < 500, the comparison is correct 'FALSE'), autoit is comparing bad this numbers, and others, while one of the numbers of the comparison gad one digit minus, make the comparison putting one zero in the right of this number with one digit minus. Example (only failed for me getting the number from a input, this is an example getting the numbers from a input):

1000 < 500

Autoit = 1000 < 5000 False

5289 < 529

Autoit = 5289 < 5290 False

if i compare the second (or first) number with 3 digits (in this example) with one zero in the left:

1000 < 0500

Autoit = 1000 < 0500 True

5289 < 0529

Autoit = 5289 < 0529 True

I repeat: The direct comparison of numbers always works fine. Always fails getting the numbers from a input in a gui (in my case).

Edited by TalivanIBM

Share this post


Link to post
Share on other sites
PsaltyDS

Stop using string compares for numeric operations. Convert the strings with Number() before comparing.

;)

Edit: Example:

$n1 = Number(GUICtrlRead($1))
Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
TalivanIBM

You have reason, i was talking with my workmate and says me the same, sorry......... I'm very impulsive.

i do not remember the function Number() thanks!!!

And sorry for the alarm.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×