io:format throws a badarg exception if the format is ~f but the argument is integer:
io:format("~f", [2]).
Adding 0.0 solves the problem bus there an elegant way?
io:format("~f", [2+0.0]).
Check if the value has a type of number and is not an integer. Check if the value is not NaN . If a value is a number, is not NaN and is not an integer, then it's a float.
In Erlang there are 2 types of numeric literals which are integers and floats.
If you don't care about the exact output, you can use:
io:format("~p", [Term]).
This will work with any term, but doesn't give you the same kind of formatting options as ~f would.
Either
io:format("~f", [2.0]).
or
io:format("~f", [float(2)]).
works.
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