How to format a number with padding in Erlang

前端 未结 4 1514
北荒
北荒 2020-12-06 09:17

I need to pad the output of an integer to a given length.

For example, with a length of 4 digits, the output of the integer 4 is \"0004\" instead of \"4\". How can I

4条回答
  •  一向
    一向 (楼主)
    2020-12-06 09:48

    The problem with io:format is that if your integer doesn't fit, you get asterisks:

    > io:format("~4..0B~n", [1234]).
    1234
    > io:format("~4..0B~n", [12345]).
    ****
    

    The problem with string:right is that it throws away the characters that don't fit:

    > string:right(integer_to_list(1234), 4, $0).
    "1234"
    > string:right(integer_to_list(12345), 4, $0).
    "2345"
    

    I haven't found a library module that behaves as I would expect (i.e. print my number even if it doesn't fit into the padding), so I wrote my own formatting function:

    %%------------------------------------------------------------------------------
    %% @doc Format an integer with a padding of zeroes
    %% @end
    %%------------------------------------------------------------------------------
    -spec format_with_padding(Number :: integer(),
                              Padding :: integer()) -> iodata().
    format_with_padding(Number, Padding) when Number < 0 ->
        [$- | format_with_padding(-Number, Padding - 1)];
    format_with_padding(Number, Padding) ->
        NumberStr = integer_to_list(Number),
        ZeroesNeeded = max(Padding - length(NumberStr), 0),
        [lists:duplicate(ZeroesNeeded, $0), NumberStr].
    

    (You can use iolist_to_binary/1 to convert the result to binary, or you can use lists:flatten(io_lib:format("~s", [Result])) to convert it to a list.)

提交回复
热议问题