Jump to content

String is Int or Float [solved]


Recommended Posts

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 by icu
Link to comment
Share on other sites

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 Gude
How 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

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 Gude
How 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

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 by icu
Link to comment
Share on other sites

@smartee

Whats 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 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

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

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

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 Gude
How 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

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 Gude
How 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

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

How correct is the helpfile in this case

StringIsInt

--------------------------------------------------------------------------------

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

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 Gude
How 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

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 Gude
How 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

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...