开发者

How to make sure a number is float in Erlang?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜