Android Question IsNumber(string) question

Hamo

Member
Licensed User
Longtime User
Does anyone know if this function just checks that all characters of the string are numbers, or does it check to see if the string can be converted to a number that B4A can handle. I mean what about this
IsNumber("432432432443243234323254325437893478932489327589327589375893257893275893275932")
Would this return true ?
Hamo
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
I did test it for you as Steve suggested. By the way, you need IsNumber not isNumeric. Sure enough, it does show that it is a number. Why in the world would you be testing for such a big number. What is the purpose? maybe you can educate us.
B4X:
If IsNumber("432432432443243234323254325437893478932489327589327589375893257893275893275932") Then
  Msgbox("Yes, I am a big number","")  'it shows that it is a number
Else
    Msgbox("No, I am not a number","")
End If
 
Upvote 0

Hamo

Member
Licensed User
Longtime User
Thanks for the replies, I guess I should have tested it instead of asking here.
I have a standalone controller which sends the status of all inputs and various flags as
zeros and ones over RS232. This controller also echoes everything it receives from the
Android tablet and sometimes the sent messages include garbage.
To filter out any garbage and validate the received string, I simply check that the long string
received is all numbers.
 
Upvote 0

Reviewnow

Active Member
Licensed User
Longtime User
The following sub will work to catch the garbage using regular expressions

Log("value Numeric " & isnumeric("432432432443243234323254325437893478932489327589327589375893257893275893275932"))
Log("value Numeric " & isnumeric("43243243244x3243234323254325437893478932489327589327589375893257893y275893275932"))

B4X:
Sub isnumeric(input As String) As Boolean
Dim MatchInput As Matcher = Regex.Matcher("^[0-9]*$", input)

If MatchInput.Find = False Then
  Return False
Else
  Return True
 
End If
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
A little bit off topic but ...

I needed something like this:
B4X:
Private Sub IsNumeric(var As Object) As Boolean
    Return ((var Is Int) OR (var Is Double) OR (var Is Long) OR (var Is Short) OR (var Is Float) OR (var Is Double) OR (var Is Byte))
End Sub

I would ask you to inform me if it is not enough or if there is a B4A function I do not know.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
How about:

B4X:
Sub IsNumeric(Var As Object) As Boolean
    Try
        Var = Var * 1
        Return True
    Catch
        Return False
    End Try
End Sub

Should also work for a numeric value in a string.
 
Last edited:
Upvote 0
Top