Lope Posted September 3, 2011 Posted September 3, 2011 (edited) I know this is a seemingly retarded question. But I've checked the manual, I've googled, and I've searched the forum. I've tried IsInt and IsNumber and @error. $foo='45' Messagebox(0,'',Int($foo)) ; returns 45 Messagebox(0,'',@error) ; returns 0, ok, thats expected... $foo='dfgfdg' Messagebox(0,'',Int($foo)) ; returns 0 Messagebox(0,'',@error) ; returns 0 < huh? $foo='45' Messagebox(0,'',IsNumber($foo)) ; returns 0... (not a numeric type) how does one check if a string converts completely to a number without errors? Edited September 3, 2011 by Lope
Rogue5099 Posted September 3, 2011 Posted September 3, 2011 (edited) Checks if a string contains only digit (0-9) characters. StringIsDigit ( "string" ) StringIsDigit("12333") ;returns 1 StringIsDigit("1.5") ;returns 0 due to decimal point StringIsDigit("1 2 3") ;returns 0 due to whitespace StringIsDigit("") ;returns 0 Edited September 3, 2011 by rogue5099 My projects: Inventory / Mp3 Inventory, Computer Stats
UEZ Posted September 3, 2011 Posted September 3, 2011 Try Number(): $w = Number(1+2+10) ;returns 13 MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$w' & @lf & @lf & 'Return:' & @lf & $w) ;### Debug MSGBOX $x = Number("3.14") ;returns 3.14 MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$x' & @lf & @lf & 'Return:' & @lf & $x) ;### Debug MSGBOX $y = Number("24/7") ;returns 24 MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$y' & @lf & @lf & 'Return:' & @lf & $y) ;### Debug MSGBOX $z = Number("tmp3") ;returns 0 MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$z' & @lf & @lf & 'Return:' & @lf & $z) ;### Debug MSGBOX Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Bowmore Posted September 3, 2011 Posted September 3, 2011 This is a function that I wrote some time ago the identifies if a value is a number. "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook
Administrators Jon Posted September 3, 2011 Administrators Posted September 3, 2011 I know this is a seemingly retarded question. But I've checked the manual, I've googled, and I've searched the forum.I've tried IsInt and IsNumber and @error. $foo='45'Messagebox(0,'',Int($foo)) ; returns 45Messagebox(0,'',@error) ; returns 0, ok, thats expected...You can't use @error like that. @error is being set by the first MessageBox so will always be 0 in this case. Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/
MvGulik Posted September 3, 2011 Posted September 3, 2011 (edited) mmm. "... (re)set first by the MessageBox() call itself so ..." Edited September 3, 2011 by iEvKI3gv9Wrkd41u "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ...
Moderators SmOke_N Posted September 4, 2011 Moderators Posted September 4, 2011 Roll your own: Func _StringIsNumber($v_num) Return (Int(StringIsInt($v_num)) + Int(StringIsFloat($v_num)) > 0) EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
SteveJM Posted November 25, 2024 Posted November 25, 2024 I know this is a long time later, but I found myself here searching for a "StringIsNumber" function. I know it could be done with a RegEx, but the resulting code can be tough to understand months later, so I came up with the following little test program and a function that works for me. I assume no malice in the input string and don't care about trailing non-numeric characters. I would be happy to know a tidier method, if someone has one. #include <StringConstants.au3> #include <Array.au3> Dim $aStr[] = ["e.g. Invalid", "0", "1234", "1.234", "1.234e-3", " 1234", "0.0", "+3", "-4.7e+4"] Dim $Disp1[UBound($aStr)][3] Dim $Disp2[UBound($aStr)][3] Dim $Disp3[UBound($aStr)][3] Dim $Disp4[UBound($aStr)][3] For $i = 0 To UBound($aStr) - 1 $Disp1[$i][0] = $aStr[$i] $Disp1[$i][1] = StringIsFloat($aStr[$i]) $Disp1[$i][2] = @error $Disp2[$i][0] = $aStr[$i] $Disp2[$i][1] = StringIsInt($aStr[$i]) $Disp2[$i][2] = @error $Disp3[$i][0] = $aStr[$i] $Disp3[$i][1] = Number($aStr[$i]) $Disp3[$i][2] = @error $Disp4[$i][0] = $aStr[$i] $Disp4[$i][1] = StringIsNumber($aStr[$i]) $Disp4[$i][2] = @error Next _ArrayDisplay($Disp1, "StringIsFloat") _ArrayDisplay($Disp2, "StringIsInt") _ArrayDisplay($Disp3, "Number") _ArrayDisplay($Disp4, "StringIsNumber") ;------------------------------------- Func StringIsNumber($s) $v = Number(StringStripWS($s, $STR_STRIPLEADING)) Return ($v <> 0 Or ( $v = 0 And StringLeft($s, 1) = "0")) EndFunc
Nine Posted November 25, 2024 Posted November 25, 2024 (edited) Sadly your code does not work with ["0abc", "-0.0"] Try this : #include <StringConstants.au3> Local $aStr[] = ["e.g. Invalid", "0", "1234", "1.234", "1.234e-3", " 1234", "0.0 ", "+3", "-4.7e+4", "0abc", "-0.0"] For $sString In $aStr ConsoleWrite($sString & " : " & StringIsNumber9($sString) & @CRLF) Next Func StringIsNumber9($sText) $sText = StringStripWS($sText, $STR_STRIPLEADING + $STR_STRIPTRAILING) Return StringIsInt($sText) Or StringIsFloat($sText) EndFunc Edited November 25, 2024 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
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