Jump to content

What is the funda behind Number and String compare?


nbala75
 Share

Recommended Posts

$A=50.2 (derived from webpage)

$B=50.20 (derived from an excel sheet)

It doesnt work when i say: ;)

If $A=$B then

Msgbox(0,"","Equal")

Endif

It doesnt work when i declare like this also: :idiot:

$A=Number(50.2)

$B=Number(50.20)

If $A=$B then

Msgbox(0,"","Equal")

Endif

It works when i do something like this: :)

$A=Number((50.2)&"0")

$B=Number(50.20)

If $A=$B then

Msgbox(0,"","Equal")

Endif

Apparently it took a long time for me to isolate this problem.....can some learned explain to me what is the funda behind this?

Link to comment
Share on other sites

Even that is doing it the hard way. You forced $a to be the same as $b and you are using a string comparison.

There are a few ways to skin this cat, here are but 2 of them

$a = 50.2
$b = 50.20

MsgBox(0, "TEST", "Using Mod(): " & Mod($a, $b) & @CRLF & "Using Subtraction: " & $a - $b & @CRLF)

In either case a return value of 0 means they are equal.

so your code would go something like this

$a = 50.2
$b = 50.20
$bRtn = False
If NOT Mod($a, $b) Then
    $bRtn = True
EndIf
MsgBox(0, "Result", $bRtn)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

nbala75,

You get the data as strings so it is not surprising that they are not equal in the first test as AutoIt looks at them as strings.

In the second test you have the wrong syntax for Number - go and look in the Help file how you should do it. ;)

Here is the proper way to test that data:

; Compare as strings - this will fail because they are not identical
$A = "50.2"
$B = "50.20"

If $A = $B Then
    MsgBox(0, "String", "Equal")
EndIf

; Convert those strings to numbers - this will work as the values are the same
$A = Number($A)
$B = Number($B)

If $A = $B Then
    MsgBox(0, "Number", "Equal")
EndIf

; Use the correct syntax to convert strings to numbers - this will work as the values are the same
$A = Number("50.2")
$B = Number("50.20")

If $A = $B Then
    MsgBox(0, "Number string", "Equal")
EndIf

Basically if you want to compare numerical values you need to get the variables into number format using Number correctly. :idiot:

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

All clear? :)

M23

Sure it is but Mod is just so simple; no conversions to do since it assumes a numerical input.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

In the second test you have the wrong syntax for Number - go and look in the Help file how you should do it. :)

M23

I might have given it wrongly here. But when i tried to covert that as Number (as per help file notations), it never worked...

But tell me one thing...in so many other places, i never used to convert it into Number or String for proper comparisons (unlike other languages), it failed here at this instance only.

I also remember to have read somewhere that for using $A=$B, we need not convert it into Number formats...not sure where i read in help file

Edited by nbala75
Link to comment
Share on other sites

Sure, thanx, will use Mods henceforth for these types :)

R U Sure?

$a = 100.4;<<< = Per accident you can find this !!!!!!!!!

$b = 50.20
$bRtn = False
If NOT Mod($a, $b) Then
    $bRtn = True
EndIf
MsgBox(0, "Result1", $bRtn)

; ===   Where as ===

If Number($a) = Number($b) Then
        MsgBox(0, 'Result', 'True', 5)
Else
        MsgBox(0, 'Result', 'False', 7)
EndIf

Added ; === Where As ===

Edited by JoHanatCent
Link to comment
Share on other sites

Sure, thanx, will use Mods henceforth for these types :rolleyes:

And another reason not to use Mod($a, $a) is:-

When $b = 1, the result is always True.

Also when $b = 0 and $a = 0 a Microsoft error is generated on my xp.

; When $b = 1, Result is always True.
$a = 2
$b = 1
MsgBox(0, "Result", Mod($a, $b) = 0)

; Error, send Microsoft an error report or not. (using xp OP)
$a = 0
$b = 0
MsgBox(0, "Result", Mod($a, $b) = 0)
Link to comment
Share on other sites

When $b = 1, Result is always True.

Error, send Microsoft an error report or not. (using xp OP)

True. Mod is a division sum so If one divide A by 1 you should get back A

Please don't send Mocrosoft an Error report because dividing A by 0=Zero is suppose to give us an error!

Link to comment
Share on other sites

Iam pretty screwed up now ;):)

The things which were working just right earlier are not working now...i think i really messed up the Number(" ") things here and there.

Tell me, can i not use String=String? Or shud i use only StringCompare ??

This is just sucking my time.. :idiot: :idiot:

Edited by nbala75
Link to comment
Share on other sites

  • Moderators

nbala75,

Just do as I suggested in my post a long way above and you will get the correct results. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...