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]开发者_开发百科).
- float(Number) convert Number to float
- list_to_float(String) convert String to float
- is_float(Term) returns true if Term is a float
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.
精彩评论