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 I don't think exists in Julia. I can think of a few different ways this might be done, but I would like to see some suggestions for the "best" way.
I think the preferred idiom is
isa(val, Number)
Normally you are interested in rounding floats, in which case
isa(val, AbstractFloat)
You can check the element's type like this:
typeof(val)<:Number
The :<
operator checks if a type is a subtype of another.
Here is a very helpful chart giving an overview of numeric types in Julia: https://en.wikibooks.org/wiki/Introducing_Julia/Types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With