icu Posted July 1, 2011 Share Posted July 1, 2011 (edited) Dear AutoIt Community, I'm trying to take a string number and check if it is an Integer or a Float. I couldn't find anything in the Help file so I wrote the following: Func StringIsIntOrFloat($s_Number) Select Case StringIsInt($s_Number) Return 1 Case StringIsFloat($s_Number) Return 1 Case Else Return 0 EndSelect EndFunc #cs Return Value Success: Returns 1. Failure: Returns 0 if string cannot be an integer or a float. #ce ; Proof of concept $s_Number = "1" If StringIsIntOrFloat($s_Number) <> 1 Then MsgBox(0, "Error", "String is NOT an Integer or a Float: " & $s_Number) $s_Number = "1.1" If StringIsIntOrFloat($s_Number) <> 1 Then MsgBox(0, "Error", "String is NOT an Integer or a Float: " & $s_Number) $s_Number = "1a" If StringIsIntOrFloat($s_Number) <> 1 Then MsgBox(0, "Error", "String is NOT an Integer or a Float: " & $s_Number) Is there a better way of checking? Thanks, -icu Edited July 1, 2011 by icu Link to comment Share on other sites More sharing options...
monoscout999 Posted July 1, 2011 Share Posted July 1, 2011 (edited) your code is good... another way to do it. If NOT StringIsInt($s_Number) and NOT StringIsFloat($s_Number) then msgbox(0, "Error", "String is NOT an Integer or a Float: " & $s_Number) Edited July 1, 2011 by monoscout999 Link to comment Share on other sites More sharing options...
icu Posted July 1, 2011 Author Share Posted July 1, 2011 @Monoscout999: Thanks for the quick reply. I came to that that solution too ;-) I was really wondering if there was something I was missing somewhere. Link to comment Share on other sites More sharing options...
omikron48 Posted July 1, 2011 Share Posted July 1, 2011 OR If Not (StringIsInt($s_Number) OR StringIsFloat($s_Number)) Then msgbox(0, "Error", "String is not an Integer nor a Float: " & $s_Number) Link to comment Share on other sites More sharing options...
BrewManNH Posted July 1, 2011 Share Posted July 1, 2011 As far as I can tell, there's no in-built StringIsNumber function which would seem to be the only way of checking if it's either a float or an integer in one test. Short of a RegEx you've pretty much hit on the way to check it. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
JohnOne Posted July 1, 2011 Share Posted July 1, 2011 I suppose you could check with StringRegExp(), but I'm not the one to ask how. Except to say you could test for any none numeric char, excluding a single decimal place. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JohnOne Posted July 1, 2011 Share Posted July 1, 2011 There is a stringisnum, but it returns false on a ".". AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
BrewManNH Posted July 1, 2011 Share Posted July 1, 2011 There's a StringIsDigit, but no StringIsNum, and as you stated, won't work for a float. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
smartee Posted July 1, 2011 Share Posted July 1, 2011 (edited) Hi, Try this:MsgBox(64, "_StringIsNum Demo", _StringIsNum("46")) Func _StringIsNum($sInput) Return StringRegExp($sInput, "^([0-9]*(\.[0-9]+){1}|[0-9]+(\.[0-9]*){0,1})$") = 1 EndFunc ;==>_StringIsNum Hope this helps -smartee EDIT: Addressed some cases and made a function Edited July 1, 2011 by smartee Link to comment Share on other sites More sharing options...
icu Posted July 1, 2011 Author Share Posted July 1, 2011 (edited) To all: Thanks everyone for the replies. I think I've got the answer I was was looking for. My personal preference is for shorter lines of code and avoiding Regex so the function StringIsIntOrFloat($s_Number) I wrote suits my needs perfectly. JohnOne hit the nail on the head about StringIsNum getting caught on the dot ".". At first I thought I was messing something up so I looked twice at the help file and figured I needed to write something myself. At the very least this page will get indexed in Google so the next person that has the same problem can copy and paste the above code examples :-) Edited July 1, 2011 by icu Link to comment Share on other sites More sharing options...
JohnOne Posted July 1, 2011 Share Posted July 1, 2011 Actually, looking at it again, its StringIsAlNum.Old peepers are not as they once were. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
GEOSoft Posted July 1, 2011 Share Posted July 1, 2011 (edited) @smarteeWhats the problem with the simple expression?Return StringRegExp($sInput, "^[\d.]+$")Edit:Actually the simple expression would fail (in certain cases) as shown above.Return StringRegExp($sInput, "^\d*\.?\d+$")Should work better but I don't have the energy to be bothered testing any of them right now. Edited July 1, 2011 by GEOSoft 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 More sharing options...
Exit Posted July 1, 2011 Share Posted July 1, 2011 just my 2 cents MsgBox(0, "_StringIsNum Demo", _StringIsNum("99")) ; true MsgBox(0, "_StringIsNum Demo", _StringIsNum("9+9")) ; false MsgBox(0, "_StringIsNum Demo", _StringIsNum("9.9")) ; true MsgBox(0, "_StringIsNum Demo", _StringIsNum("9 9")) ; false Func _StringIsNum($s_Number) Return StringIsInt($s_Number) + StringIsFloat($s_Number) EndFunc ;==>_StringIsNum regards Forumer100 App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
JohnOne Posted July 1, 2011 Share Posted July 1, 2011 just my 2 cents MsgBox(0, "_StringIsNum Demo", _StringIsNum("99")) ; true MsgBox(0, "_StringIsNum Demo", _StringIsNum("9+9")) ; false MsgBox(0, "_StringIsNum Demo", _StringIsNum("9.9")) ; true MsgBox(0, "_StringIsNum Demo", _StringIsNum("9 9")) ; false Func _StringIsNum($s_Number) Return StringIsInt($s_Number) + StringIsFloat($s_Number) EndFunc ;==>_StringIsNum regards Forumer100 That's quite an ace way that. Brilliant in its simplicity. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
BrewManNH Posted July 1, 2011 Share Posted July 1, 2011 Can someone explain what the difference is between StringIsDigit and StringIsInt? They both seem to do the exact same comparisons and I don't see what the use of one over the other would be for. If anyone has any insight on it, I'd appreciate it. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
BrewManNH Posted July 1, 2011 Share Posted July 1, 2011 just my 2 cents MsgBox(0, "_StringIsNum Demo", _StringIsNum("99")) ; true MsgBox(0, "_StringIsNum Demo", _StringIsNum("9+9")) ; false MsgBox(0, "_StringIsNum Demo", _StringIsNum("9.9")) ; true MsgBox(0, "_StringIsNum Demo", _StringIsNum("9 9")) ; false Func _StringIsNum($s_Number) Return StringIsInt($s_Number) + StringIsFloat($s_Number) EndFunc ;==>_StringIsNum regards Forumer100 Very slick by the way. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
trancexx Posted July 1, 2011 Share Posted July 1, 2011 Can someone explain what the difference is between StringIsDigit and StringIsInt? They both seem to do the exact same comparisons and I don't see what the use of one over the other would be for. If anyone has any insight on it, I'd appreciate it.StringIsDigit checks for digits and StringIsInt checks if a string is integer representation."-12" is int but "-" is not a digit, i.e. StringIsDigit will be 0. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
JohnOne Posted July 1, 2011 Share Posted July 1, 2011 How correct is the helpfile in this caseStringIsInt --------------------------------------------------------------------------------Checks if a string is an integer.If tested with "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" will return true. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
BrewManNH Posted July 1, 2011 Share Posted July 1, 2011 It's an integer in regards to that it's a number without a decimal point, it may not be an INT as defined by a programmer, but it's an integer as defined by a mathematician. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
BrewManNH Posted July 1, 2011 Share Posted July 1, 2011 StringIsDigit checks for digits and StringIsInt checks if a string is integer representation."-12" is int but "-" is not a digit, i.e. StringIsDigit will be 0.I see that now. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
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