Jump to content

Test if String is a Number


Lope
 Share

Recommended Posts

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

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

Link to comment
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

Link to comment
Share on other sites

  • Administrators

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

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

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