zoukankan      html  css  js  c++  java
  • Erlang 中类型转换

    文档中给出的数据转换例子 罗列如下

    源地址: http://www.erlang.org/doc/reference_manual/data_types.html

    atom_to_list(hello)
    "hello"
    list_to_atom("hello").
    hello
    binary_to_list(<<"hello">>).
    "hello"
    binary_to_list(<<104,101,108,108,111>>).
    "hello"
    list_to_binary("hello").
    <<104,101,108,108,111>>
    integer_to_list(77).
    "77"
    list_to_integer("77").
    77
    tuple_to_list({a,b,c}).
    [a,b,c]
    list_to_tuple([a,b,c]).
    {a,b,c}
    term_to_binary({a,b,c}).
    <<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>
    binary_to_term(<<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>).
    {a,b,c}

    重点看一下 float_to_list/1

    float_to_list(7.0).
    "7.00000000000000000000e+00"
    list_to_float("7.000e+00").
    7.0
    19> float_to_list(7.12, [{decimals, 4}]).
    "7.1200"
    20> float_to_list(7.12).
    "7.12000000000000010658e+00"
    21> float_to_list(7.12,[compact]).
    "7.12000000000000010658e+00"
    22> float_to_list(7.12, [{decimals, 6},compact]).
    "7.12"
    23> float_to_list(7.1234567, [{decimals, 6},compact]).
    "7.123457"
    24> float_to_list(7.12345, [{decima
    ls, 6},compact]).
    "7.12345

    常用

    
    %%--------------------------------------------------------------------
    %% @doc
    %% Converts to list
    %% @end
    %%--------------------------------------------------------------------
    any_to_list(undefined) ->
        "";
    any_to_list(List) when is_list(List) ->
        List;
    any_to_list(Bin) when is_binary(Bin) ->
        case unicode:characters_to_binary(Bin, utf8, utf8) of
            Bin -> unicode:characters_to_list(Bin);
            _ -> binary_to_list(Bin)
        end;
    any_to_list(Atom) when is_atom(Atom) ->
        atom_to_list(Atom);
    any_to_list(Number) when is_integer(Number) ->
        integer_to_list(Number);
    any_to_list(_) ->
        throw(badarg).
    %%--------------------------------------------------------------------
    %% @doc
    %% Converts to atom
    %% @end
    %%--------------------------------------------------------------------
    any_to_atom(Atom) when is_atom(Atom) ->
        Atom;
    any_to_atom(List) when is_list(List) ->
        list_to_atom(List);
    any_to_atom(Bin) when is_binary(Bin) ->
        list_to_atom(any_to_list(Bin));
    any_to_atom(_) ->
        throw(badarg).
    %%--------------------------------------------------------------------
    %% @doc
    %% Converts to binary
    %% @end
    %%--------------------------------------------------------------------
    any_to_binary(Bin) when is_binary(Bin)->
        Bin;
    any_to_binary(List) when is_list(List) ->
        case unicode:characters_to_binary(List) of
            {error,_,_} -> list_to_binary(List);
            B -> case unicode:characters_to_list(B,utf8) of
                     List -> B;
                     _ -> list_to_binary(List)
                 end
        end;
    any_to_binary(Atom) when is_atom(Atom) ->
        erlang:atom_to_binary(Atom, utf8);
    any_to_binary(_) ->
        throw(badarg).
    %%--------------------------------------------------------------------
    %% @doc
    %% convert to integer
    %% @spec
    %% @end
    %%--------------------------------------------------------------------
    any_to_integer(Int) when is_integer(Int) ->
        Int;
    any_to_integer(List) when is_list(List) ->
        list_to_integer(List);
    any_to_integer(Bin) when is_binary(Bin) ->
        list_to_integer(any_to_list(Bin));
    any_to_integer(Float) when is_float(Float) ->
        trunc(Float);  %%round(Float);
    any_to_integer(_) ->
        throw(badarg).
    
  • 相关阅读:
    [Jweb] Servlet 生命周期, TestLifeCycleServlet extends HttpServlet
    [Java] Javadoc jdk 下载地址
    [Jweb] 在 servlet 中重写 service() 方法,不重写 doGet, doPost 方法,示例。
    [Jweb] Servlet / GenericServlet / HttpServlet 及其方法示意图
    [Jweb] 第一个通过 tomcat 配置,浏览器访问的 web 界面
    [Jweb] tomcat 知识点 (from bjsxt ZZY)
    [Jweb] HTTP 1.1与HTTP 1.0的比较 (from bjsxt 张志宇)
    post方式提交数据
    php页面获取数据库中的数据
    表单输入框样式
  • 原文地址:https://www.cnblogs.com/ShankYan/p/4141803.html
Copyright © 2011-2022 走看看