Number is a more general type which refers to both floats and integers. If you want to check if a value is numeric, but don't care if it's a float or an integer, use number. If you want to enforce a specific type, use integer/float. In your example above you could use is_integer(x)
or is_float(x)
instead of is_number(x)
if you need more specificity.
Elixir: when to use 'number' and when to use 'integer'
-
21-07-2023 - |
Question
The examples seem to consist of both 'number' and 'integer'. What are the respective situations that they should be used in?
Edit: here is an example of each
@type bar :: %{atom => [{binary, integer}]}
def foo(x) when is_number(x), do: x
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow