How to check numerical value in Julia

I want to determine if a value is numeric or not before trying to use a function on it. As a specific example:

z = [1.23,"foo"] for val in z if isnumeric(val) round(z) end end 

Here isnumeric() is a function that, it seems to me, does not exist in Julia. I can come up with several different ways that this can be done, but I would like to see some suggestions for the β€œbest” way.

+7
types julia-lang
source share
2 answers

I think the preferred idiom

 isa(val, Number) 

Usually you are interested in rounded floats, in which case

 isa(val, AbstractFloat) 
+9
source share

You can check the item type as follows:

 typeof(val)<:Number 

The operator :< checks whether the type is a subtype of another.

Here is a very useful diagram giving an overview of numeric types in Julia: https://en.wikibooks.org/wiki/Introducing_Julia/Types

+8
source share

All Articles