Handling Errors - Input variable type

Hello. I am starting coding in Elixir right now and I have some questions.
I am creating a simple script and want to handle with incorrect type of inputs.
Let’s imagine I have this simple function:

def currencyConverter({ from, to, amount }) do
    result = exchangeConversion({ from, to, amount })
    exchangeResult = resultParser(result)

And I need to guarantee that “from” and “to” are strings and “amount” is boolean and want to handle this error sending customized messages instead explode an erlang error in the terminal
Which is the best way? use an if is_float(amount) … else, or I have a more elegant ways?

The go-to tooling for data validation and casting is Ecto’s Changeset module.

Please reconsider your strategy around a data type for a currency amount. A Decimal would be much preferred.

Lastly, there are a few money packages around that could provide the guarantees you’re after. One of which I am the author (it’s ex_money).

1 Like

Hello Kip.
I am doing an code challenge for an interview, and they asked me to build a script to make currency converter, separate integer value and decimal value like this: 3.53 -> { integer: 3 , decimal: 53} to make calculation. Should I forget float and use decimal for this approach?