Trong Posted 9 hours ago Posted 9 hours ago (edited) I was debugging my script and felt it was not working properly and found out it was due to the IsNumber() function. Is this a bug? Let's see the example code and the output: expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; This script demonstrates the behavior of the IsNumber() function. ConsoleWrite("--\\ AutoIt v" & @AutoItVersion & (@AutoItX64 = 1 ? " _x64" : " x86") & " OSArch: " & @OSArch & " //-- " & @CRLF) ; Open the console to view the output ConsoleWrite("--- Testing the IsNumber() Function ---" & @CRLF & @CRLF) ; Test Case 1: A string containing a valid integer Local $sVar1 = "123" ConsoleWrite("1. Testing string: '" & $sVar1 & "'" & @CRLF) ConsoleWrite("! > IsNumber() returns: " & IsNumber($sVar1) & " (Note: 1 means True)" & @CRLF & @CRLF) ; Test Case 2: A string containing a negative number Local $sVar2 = "-45" ConsoleWrite("2. Testing string: '" & $sVar2 & "'" & @CRLF) ConsoleWrite("! > IsNumber() returns: " & IsNumber($sVar2) & " (1 = True)" & @CRLF & @CRLF) ; Test Case 3: A string containing a floating-point number Local $sVar3 = "98.6" ConsoleWrite("3. Testing string: '" & $sVar3 & "'" & @CRLF) ConsoleWrite("! > IsNumber() returns: " & IsNumber($sVar3) & " (1 = True)" & @CRLF & @CRLF) ; Test Case 4: An actual number (integer) variable Local $nVar4 = 500 ConsoleWrite("4. Testing a numeric variable: " & $nVar4 & @CRLF) ConsoleWrite("+ > IsNumber() returns: " & IsNumber($nVar4) & " (1 = True)" & @CRLF & @CRLF) ; Test Case 5: A string containing numbers AND text Local $sVar5 = "76 trombones" ConsoleWrite("5. Testing a mixed string: '" & $sVar5 & "'" & @CRLF) ConsoleWrite("- > IsNumber() returns: " & IsNumber($sVar5) & " (Note: 0 means False)" & @CRLF & @CRLF) ; Test Case 6: A string containing only text Local $sVar6 = "Hello World" ConsoleWrite("6. Testing a text-only string: '" & $sVar6 & "'" & @CRLF) ConsoleWrite("- > IsNumber() returns: " & IsNumber($sVar6) & " (0 = False)" & @CRLF & @CRLF) ; --- Practical Usage Example --- ConsoleWrite("--- Practical Usage Example ---" & @CRLF) Local $sUserInput = "99" ; Simulate user input that is always a string If IsNumber($sUserInput) Then ConsoleWrite("'" & $sUserInput & "' is a valid number. Proceeding with calculation." & @CRLF) Local $iResult = Number($sUserInput) + 1 ConsoleWrite(" Calculation result ($sUserInput + 1) = " & $iResult & @CRLF) Else ConsoleWrite("! '" & $sUserInput & "' is NOT a valid number." & @CRLF & @CRLF) EndIf Output: --\\ AutoIt v3.3.16.1 _x64 OSArch: X64 //-- --- Testing the IsNumber() Function --- 1. Testing string: '123' ! > IsNumber() returns: 0 (Note: 1 means True) 2. Testing string: '-45' ! > IsNumber() returns: 0 (1 = True) 3. Testing string: '98.6' ! > IsNumber() returns: 0 (1 = True) 4. Testing a numeric variable: 500 + > IsNumber() returns: 1 (1 = True) 5. Testing a mixed string: '76 trombones' - > IsNumber() returns: 0 (Note: 0 means False) 6. Testing a text-only string: 'Hello World' - > IsNumber() returns: 0 (0 = False) --- Practical Usage Example --- ! '99' is NOT a valid number. I had to create a replacement function: Func _IsNumber($vValue, $iMode = -1) $vValue = StringStripWS($vValue, 8) Switch $iMode Case 0 ; Mode 0: The string must start with a number. Local $sPattern = "^\s*[+-]?[0-9]" Return StringRegExp($vValue, $sPattern) = 1 Case 1 ; Mode 1: The string contains at least one digit anywhere. Local $sPattern = "\d" Return StringRegExp($vValue, $sPattern) = 1 Case Else ; Default to strict mode if an invalid mode is provided. ; Mode -1: The entire string must be a valid number (decimal, hex, or scientific). Local $sPattern = "(^[+-]?[0-9]+\.?[0-9]*$|^0x[0-9A-Fa-f]{1,8}$|^[0-9]+\.?[0-9]*[Ee][+-][0-9]+$)" Return StringRegExp($vValue, $sPattern) = 1 EndSwitch EndFunc ;==>_IsNumber Give your opinion! Edited 8 hours ago by Trong add function Regards,
argumentum Posted 8 hours ago Posted 8 hours ago ConsoleWrite_IsNumber(5) ConsoleWrite_IsNumber(-5) ConsoleWrite_IsNumber(-0) ConsoleWrite_IsNumber("-5") ConsoleWrite_IsNumber("-0") ConsoleWrite_IsNumber("hello") Func ConsoleWrite_IsNumber($vValue) Local $iResult = IsNumber($vValue) ConsoleWrite(($iResult ? "+" : "-") & " Value: " & $vValue & @TAB & " Result"& $iResult & @TAB & " VarType: " & VarGetType($vValue) & @TAB & " IsNumericString: " & _IsNumericString($vValue) & @CRLF) EndFunc Func _IsNumericString($sString) ; https://www.autoitscript.com/forum/topic/213007-is-isnumber-working-as-designed/ ; This expression checks whether a string is an integer or a real number, possibly with a negative sign. Return StringRegExp($sString, '^\-?\d+(\.\d+)?$') = 1 EndFunc ;==>_IsNumericString Exit + Value: 5 Result1 VarType: Int32 IsNumericString: True + Value: -5 Result1 VarType: Int32 IsNumericString: True + Value: 0 Result1 VarType: Int32 IsNumericString: True - Value: -5 Result0 VarType: String IsNumericString: True - Value: -0 Result0 VarType: String IsNumericString: True - Value: hello Result0 VarType: String IsNumericString: False According to the help file it behaves as it should. Your function is handy to have. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
ioa747 Posted 8 hours ago Posted 8 hours ago IsNumber ( variable ) Checks if a variable's base type is numeric. vs StringIsDigit ( "string" ) Checks if a string contains only digit (0-9) characters. I know that I know nothing
Trong Posted 8 hours ago Author Posted 8 hours ago 9 minutes ago, argumentum said: ConsoleWrite_IsNumber(5) ConsoleWrite_IsNumber(-5) ConsoleWrite_IsNumber(-0) ConsoleWrite_IsNumber("-5") ConsoleWrite_IsNumber("-0") ConsoleWrite_IsNumber("hello") Func ConsoleWrite_IsNumber($vValue) Local $iResult = IsNumber($vValue) ConsoleWrite(($iResult ? "+" : "-") & " Value: " & $vValue & @TAB & " Result"& $iResult & @TAB & " VarType: " & VarGetType($vValue) & @TAB & " IsNumericString: " & _IsNumericString($vValue) & @CRLF) EndFunc Func _IsNumericString($sString) ; https://www.autoitscript.com/forum/topic/213007-is-isnumber-working-as-designed/ ; This expression checks whether a string is an integer or a real number, possibly with a negative sign. Return StringRegExp($sString, '^\-?\d+(\.\d+)?$') = 1 EndFunc ;==>_IsNumericString Exit + Value: 5 Result1 VarType: Int32 IsNumericString: True + Value: -5 Result1 VarType: Int32 IsNumericString: True + Value: 0 Result1 VarType: Int32 IsNumericString: True - Value: -5 Result0 VarType: String IsNumericString: True - Value: -0 Result0 VarType: String IsNumericString: True - Value: hello Result0 VarType: String IsNumericString: False According to the help file it behaves as it should. Your function is handy to have. Normally when dealing with data it is often extracted using strings, I think it should be designed to work with strings! 3 minutes ago, ioa747 said: IsNumber ( variable ) Checks if a variable's base type is numeric. vs StringIsDigit ( "string" ) Checks if a string contains only digit (0-9) characters. It seems so limited! Regards,
argumentum Posted 8 hours ago Posted 8 hours ago Just now, Trong said: I think it should be designed to work with strings! I don't disagree with a need for such function but your title is "Is IsNumber() working as designed?" and your question is "Is this a bug?". The answer is that it works as designed. No bug. Should have title it "The one function that every one needs" and a link go a lengthy misleading video of your rant Like and subscribe Trong 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Trong Posted 8 hours ago Author Posted 8 hours ago 1 minute ago, argumentum said: I don't disagree with a need for such function but your title is "Is IsNumber() working as designed?" and your question is "Is this a bug?". The answer is that it works as designed. No bug. Should have title it "The one function that every one needs" and a link go a lengthy misleading video of your rant Like and subscribe Yes, I accept that it is not a bug! I mean the way it is designed to work. Does it make sense to check if "a number" is a number but "its data type is a number"? Shouldn't it accept strings of numbers? argumentum 1 Regards,
argumentum Posted 8 hours ago Posted 8 hours ago I've pushed for changes in UDFs and AutoIt3 it self many a time and, some got accepted and some got declined. Make the a function that is obviously awesome ( I guess you did above ), present it and, ..wait ?. At times I discover code ( from years ago ! ) and am like, where was this when I needed it ! Open a post in examples and at least it gets exposure. I know that the functionality you present is useful and welcomed. PS: Here at AutoIt, you'll see that no one wants to brake backwards compatibility ( script braking changes ), so anything to be added/changed should keep this in mind Trong 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Trong Posted 8 hours ago Author Posted 8 hours ago Actually, there have been many topics discussing IsNumber and its problems, and every time we code, we sometimes make the same old mistake of using IsNumber in a way that is not designed 🤣 argumentum 1 Regards,
Numeric1 Posted 7 hours ago Posted 7 hours ago Personally, I find that IsNumber() is a clear, reliable, and well-designed function. It avoids the pitfalls of implicit conversions and adheres to the single responsibility principle. For these reasons, it fits perfectly within the logic of the AutoIt language. There is a whole range of related functions, such as Number, StringIsDigit, and others, which provide precise and consistent return values. This allows developers to maintain control over data processing and avoid logical or interpretation errors.
argumentum Posted 5 hours ago Posted 5 hours ago (edited) ConsoleWrite_IsNumber("6" + "6" + "-24" + 6) ; + Value: -6 Result1 VarType: Double IsNumericString: True Func ConsoleWrite_IsNumber($vValue) Local $iResult = IsNumber($vValue) ConsoleWrite(($iResult ? "+" : "-") & " Value: " & $vValue & @TAB & " Result"& $iResult & @TAB & " VarType: " & VarGetType($vValue) & @TAB & " IsNumericString: " & _IsNumericString($vValue) & @CRLF) EndFunc StringIsDigit Func _IsNumericString($sString) ; https://www.autoitscript.com/forum/topic/213007-is-isnumber-working-as-designed/ ; This expression checks whether a string is an integer or a real number, possibly with a negative sign. Return StringRegExp($sString, '^\-?\d+(\.\d+)?$') = 1 EndFunc ;==>_IsNumericString Exit AutoIt is very intuitive. There are things that just works, ....even if "wrong". But as with anything, it takes experience with the language. PS: JavaScript. Now that is..., good luck with that ! ( https://jsdate.wtf/ ) Edited 5 hours ago by argumentum added "for LOLs" Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now