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

  • 13 years later...

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

 

Link to comment
Share on other sites

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