Sign in to follow this  
Followers 0
Lope

Test if String is a Number

7 posts in this topic

#1 ·  Posted (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 by Lope

Share this post


Link to post
Share on other sites



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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

You can't use @error like that. @error is being set by the first MessageBox so will always be 0 in this case.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

mmm. "... (re)set first by the MessageBox() call itself so ..."

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0