Jump to content

Validate that a variable contains a number


Uten
 Share

Recommended Posts

I'm probably in the stupid corner today so pleas excuse me if I have overseen the obvious.

I want to make sure a variable contains a number so I can decide if I should do a numeric comparison or a character based one.

My first try was to use number($arg) andIsNumeric($arg) but there are trouble in heaven

number("21j") Will return 21 as explained in the help file. And IsNumeric($arg) is false if $arg is initiated with "21".

So, I have created this function and validating tests. Can you do better? Using StringRegExp (like I have done) to validate if the argument seems like overkill to me ;)

Func NumNumeric($arg)
    ;PURPOSE: Check if argument is numeric.
    ;TODO: How about hex, oct ...(probably use flags)?
    Return NOT StringRegExp($arg, "[^\d]+") ;Number will return 0=false for 0
EndFunc
Func assert($bool, $msg = "Unexpected result", $erl=@ScriptLineNumber)
    if not $bool then 
        ConsoleWrite("(" & $erl & ") := " & $msg & @LF)
    EndIf 
EndFunc 
Func testNumNumeric()
    assert(NumNumeric(0))
    assert(NumNumeric("0"))
    assert(NumNumeric(2))
    assert(NumNumeric("2"))
    assert(NOT NumNumeric("21e"))
    assert(NOT NumNumeric("21j"))
    ConsoleWrite("+++DONE: testNumNumeric" & @LF)
EndFunc

Your input is appreciated

Link to comment
Share on other sites

I'm probably in the stupid corner today so pleas excuse me if I have overseen the obvious.

I want to make sure a variable contains a number so I can decide if I should do a numeric comparison or a character based one.

My first try was to use number($arg) andIsNumeric($arg) but there are trouble in heaven

number("21j") Will return 21 as explained in the help file. And IsNumeric($arg) is false if $arg is initiated with "21".

So, I have created this function and validating tests. Can you do better? Using StringRegExp (like I have done) to validate if the argument seems like overkill to me ;)

Func NumNumeric($arg)
    ;PURPOSE: Check if argument is numeric.
    ;TODO: How about hex, oct ...(probably use flags)?
    Return NOT StringRegExp($arg, "[^\d]+") ;Number will return 0=false for 0
EndFunc
Func assert($bool, $msg = "Unexpected result", $erl=@ScriptLineNumber)
    if not $bool then 
        ConsoleWrite("(" & $erl & ") := " & $msg & @LF)
    EndIf 
EndFunc 
Func testNumNumeric()
    assert(NumNumeric(0))
    assert(NumNumeric("0"))
    assert(NumNumeric(2))
    assert(NumNumeric("2"))
    assert(NOT NumNumeric("21e"))
    assert(NOT NumNumeric("21j"))
    ConsoleWrite("+++DONE: testNumNumeric" & @LF)
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Thanks for your suggestions @gafrost and @JdeB. :evil:

I just new my solution was on the far left and beyond.... I just did not have the imagination to check for other String* functions when I was looking for a number.:">

StringIsDigit() and StringIsInt() worked with my tests.

EDIT: And there even is a StringIsAlNum(...) :lmao:;)

Edited by Uten
Link to comment
Share on other sites

Uh, Should not this return 0??

StringIsAlNum("21e")

Even the sample in the help file is a bot odd. Or is it time to call it a day for me?

Link to comment
Share on other sites

Yup, definitely time for bed. I thought alphanumeric was only numbers but Merriam-webster says othervice.

Link to comment
Share on other sites

Try this:

Dim $array[7] = [6, '12345', 67890, 'a12345', '12345b', '123c45', 'abcdef']

For $i = 1 To $array[0]
    MsgBox(0, $i & ' of 6', $array[$i] & ' = ' & _charcheck($array[$i]))
Next

Func _charcheck($var)
    If StringRegExp($var, '[:alpha:]') Then
        Return 'letters or mixed'
    Else
        Return 'numbers only'
    EndIf
EndFunc

edit -

I just noticed that this does not work in 3.2.1.13, but it works perfectly fine in 3.2.0.1.

Edited by xcal
Link to comment
Share on other sites

Try this: .....

You did not read the first post did you ;)

v3.2.0.1 Used the home brewed regexp engine. The latest beta +3.2.1.8 (I think) uses the pcre implementation. There has been some report of failling classes in the pcre implementation. You are encurraged to try the pcretest.exe on your regexp before complaining. Could be the reason it does not work.

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