Jump to content

IsNumerical


Bowmore
 Share

Recommended Posts

I would like to share this little function with you. I have found it very useful for validating the input of various functions where the input must be a numerical value.

I hope you find it useful

Dim $aNumbers[21]
$aNumbers[1] = 12345
$aNumbers[2] = "-12345"
$aNumbers[3] = +12345.678
$aNumbers[4] = "12345.678"
$aNumbers[5] = "One"
$aNumbers[6] = ""
$aNumbers[7] = "123.One"
$aNumbers[8] = 0xFF1165AB
$aNumbers[9] = "0x0000Ff"
$aNumbers[10] = 0x0000
$aNumbers[11] = "0x0000GG"
$aNumbers[12] = 4.395179e+003
$aNumbers[13] = "4.395179e+003"
$aNumbers[14] = 4.395179e-003
$aNumbers[15] = "4.395179e-003"
$aNumbers[16] = 4e-003
$aNumbers[17] = 4E+003
$aNumbers[18] = "4e-003"
$aNumbers[19] = "4E+003"

For $i = 1 To 20 Step 1
  MsgBox(0,"IsNumeric", '$aNumbers['& $i & '] = ' & $aNumbers[$i] & @CRLF & @CRLF & 'IsNumber() = ' & IsNumber($aNumbers[$i]) & @CRLF & @CRLF & 'IsString() = ' & IsString($aNumbers[$i])  & @CRLF & @CRLF & 'Number() = ' & Number($aNumbers[$i]) & @CRLF & @CRLF & '_IsNumerical() = ' & _IsNumerical($aNumbers[$i]) & @crlf )
Next

; #FUNCTION# ==============================================================
; Name...........: _IsNumerical
; Description ...: Uses a regular expression to check if $vValue can be interpreted as a numeric value
; Syntax.........: _IsNumerical($vValue)
; Parameters ....: $vValue - Value to test if it is numerical
; Return values .: True - If $vValue contains a numeric value
; ...............: False- If $vValue contains a non-numeric value
; Author ........: Bowmore <bowmore at lineone dot net>
; Modified.......:
; Remarks .......: Accepts values in Decimal, Hex and Exponential format. If $vValue is a string then
;                  the whole string must be numeric value for the function to return True
; Related .......: IsNumber, Number
; Link ..........:
; Example .......: Yes
; ===============================================================
Func _IsNumerical($vValue)
  If IsNumber($vValue) Then Return True
  If StringRegExp($vValue, "(^[+-]?[0-9]+\.?[0-9]*$|^0x[0-9A-Fa-f]{1,8}$|^[0-9]+\.?[0-9]*[Ee][+-][0-9]+$)") Then Return True
  Return False
EndFunc

Edit: Added IsNumber() check to speed things up a bit.

Edited by Bowmore

"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

Brilliant stuff!!!

I had a need for something after having similar problems

with IsNumber() and ended up making a crude workaround

Yours is MUCH better.

Thanks for this function, I am already using it in my script.

Link to comment
Share on other sites

Nice function Bowmore.

I think you need to allow for decimal points in the exponent though.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Nice function Bowmore.

I think you need to allow for decimal points in the exponent though.

Martin, I did think about that but when using E notation only integer exponent values are valid.

"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

Martin, I did think about that but when using E notation only integer exponent values are valid.

Oh, I didn't know that.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Bowmore

A good idea nicely done.

A few thoughts :-

Your lines, "============", are too long. It splits and drops down to the next line. They cause an error when script is run in SciTE. I was told this, and I am passing it on.

0x000f is an acceptable Hex value, and some users do use lower case hex value lettering. I added "a-f" to the regular expression for my benefit to cover that scenario. I can not see this addition causing problems elsewhere.

The example is the result of my testing and seeing what's possible.

;
; http://www.autoitscript.com/forum/index.php?s=&showtopic=97315&view=findpost&p=699566
;IsNumeric :-
;  (StringRegExp($aNumbers[$i], "(^[+-]?[0-9]+\.?[0-9]*$|^0x[0-9A-Fa-f]{1,8}$|^[0-9]+\.?[0-9]*[Ee][+-][0-9]+$)") = 1)
; evaluates as True or False
 
 Dim $aNumbers[6]
 $aNumbers[0] = (2 * 2) ^ 2.5
 $aNumbers[1] = "(2 * 2) ^ 2.5"
 $aNumbers[2] = Execute("(2 * 2) ^ 2.5")
 $aNumbers[3] = StringFormat("%o", 10); Octal
 $aNumbers[4] = 0x000f
 $aNumbers[5] = "0x000f"
 
 For $i = 0 To UBound($aNumbers) - 1
    MsgBox(0, "IsNumeric", '$aNumbers[' & $i & '] = ' & $aNumbers[$i] & @CRLF & @CRLF & 'IsNumber() = ' & _
            IsNumber($aNumbers[$i]) & @CRLF & @CRLF & 'IsString() = ' & IsString($aNumbers[$i]) & @CRLF & @CRLF & _
            'Number() = ' & Number($aNumbers[$i]) & @CRLF & @CRLF & '_IsNumerical() = ' & _
            (StringRegExp($aNumbers[$i], "(^[+-]?[0-9]+\.?[0-9]*$|^0x[0-9A-Fa-f]{1,8}$|^[0-9]+\.?[0-9]*[Ee][+-][0-9]+$)") = 1) & @CRLF)
 Next
 
;Modified  http://www.autoitscript.com/forum/index.php?s=&showtopic=97315&view=findpost&p=699566
 Func _IsNumerical($vValue)
    Return (StringRegExp($vValue, "(^[+-]?[0-9]+\.?[0-9]*$|^0x[0-9A-Fa-f]{1,8}$|^[0-9]+\.?[0-9]*[Ee][+-][0-9]+$)") = 1)
 EndFunc  ;==>_IsNumerical
;

Thanks for sharing.

Link to comment
Share on other sites

It works for string representations fo numbers, too, but I would add a IsNumber to make it faster on variables with real numeric type.

;Modified  http://www.autoitscript.com/forum/index.php?s=&showtopic=97315&view=findpost&p=699566
; Author ........: Bowmore <bowmore at lineone dot net>
; Modified.......: Malkey, Prog@ndy
Func _IsNumerical($vValue)
    If IsNumber($vValue) Then Return True
    Return (StringRegExp($vValue, "(^[+-]?[0-9]+\.?[0-9]*$|^0x[0-9A-Fa-f]{1,8}$|^[0-9]+\.?[0-9]*[Ee][+-][0-9]+$)") = 1)
EndFunc  ;==>_IsNumerical
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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