Jump to content

Check if the data is numeric or not: IsNumber() IsInt() not work ?


Trong
 Share

Go to solution Solved by AlessandroAvolio,

Recommended Posts

Eg 1:

Global $exString = '1100'
ConsoleWrite("StringIsDigit(" & $exString & ")=" & StringIsDigit($exString) & @CRLF)
ConsoleWrite("IsInt(" & $exString & ")=" & IsInt($exString) & @CRLF)
ConsoleWrite("IsNumber(" & $exString & ")=" & IsNumber($exString) & @CRLF)
MsgBox(0, "Is Numerical ?","StringIsDigit(" & $exString & ")=" & StringIsDigit($exString) & @CRLF & "IsInt(" & $exString & ")=" & IsInt($exString) & @CRLF & "IsNumber(" & $exString & ")=" & IsNumber($exString))

image.png.a676c0ea625432e7ee588111595e9be6.png

EG2:

if treat variable as number it works but value is not correct because i am checking input data is numeric or string:

;Global $exString = 'xx1100'
Global $exString = '1100xx'
 $exString = Number($exString)
ConsoleWrite("StringIsDigit(" & $exString & ")=" & StringIsDigit($exString) & @CRLF)
ConsoleWrite("IsInt(" & $exString & ")=" & IsInt($exString) & @CRLF)
ConsoleWrite("IsNumber(" & $exString & ")=" & IsNumber($exString) & @CRLF)
MsgBox(0, "Is Numerical ?","StringIsDigit(" & $exString & ")=" & StringIsDigit($exString) & @CRLF & "IsInt(" & $exString & ")=" & IsInt($exString) & @CRLF & "IsNumber(" & $exString & ")=" & IsNumber($exString))

image.png.b3ade7e39c6a9e946b381c1e39ed12ac.png

 

 

 

 

image.png

Edited by VIP
ss

Regards,
 

Link to comment
Share on other sites

I found this UDF to be a perfect alternative, but not the intent of my question:

Code quote:

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

 

Edited by VIP

Regards,
 

Link to comment
Share on other sites

29 minutes ago, VIP said:

if treat variable as number it works but value is not correct because i am checking input data is numeric or string:

Your question isn't clear. When and why do you find "value is not correct".

Which input formats do you want to accept or filter out?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Sorry for the unclear question.
I don't understand what AutoIT's IsNumber function is used for? if the input data type is already numeric then it must be numeric, up I think it has to compare with different data types, specifically here is string.

Regards,
 

Link to comment
Share on other sites

;Global $exString = 'xx1100'
Global $exString = '1100xx'
$exString = Number($exString)

MsgBox(0, "", "Var(" & $exString & ") is : " & VarGetType($exString))

The Number func doesnt convert the given var into a Number datatype but into Int32, Int64 or Double.

https://www.autoitscript.com/autoit3/docs/functions/Number.htm

 

Also check out https://www.autoitscript.com/autoit3/docs/intro/lang_datatypes.htm

Edited by Rurorita
Link to comment
Share on other sites

  • Solution

IsNumber asks whether the content of the variable is meant to represent a numeric quantity. A string is not meant to represent a number but a series of characters.

IsNumber(variable) ... -> "variable" can be a number, array, string, pointer, boolean.

Edited by AlessandroAvolio
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

×
×
  • Create New...