开发者

How to make sure a number is float in Erlang?

开发者 https://www.devze.com 2023-02-14 02:56 出处:网络
io:format throws a badarg exception if the format is ~f but the argument is integer: io:format(\"~f\", [2]).

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消