如何在 Erlang 中使用填充来格式化数字

发布于 2024-07-30 11:54:03 字数 88 浏览 4 评论 0原文

我需要将整数的输出填充到给定长度。

例如,长度为4位,整数4的输出是“0004”而不是“4”。 我怎样才能在 Erlang 中做到这一点?

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 do this in Erlang?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼眸里的那抹悲凉 2024-08-06 11:54:03

对 Zed 的答案添加一些解释:

Erlang 格式规范 是:~ FPPadModC。

"~4..0B~n" 翻译为:

 ~F. = ~4.  (Field width of 4)
  P. =   .  (no Precision specified)
Pad  =  0   (Pad with zeroes)
Mod  =      (no control sequence Modifier specified)
  C  =  B   (Control sequence B = integer in default base 10)

并且 ~n 是新行。

adding a bit of explanation to Zed's answer:

Erlang Format specification is: ~F.P.PadModC.

"~4..0B~n" translates to:

 ~F. = ~4.  (Field width of 4)
  P. =   .  (no Precision specified)
Pad  =  0   (Pad with zeroes)
Mod  =      (no control sequence Modifier specified)
  C  =  B   (Control sequence B = integer in default base 10)

and ~n is new line.

天荒地未老 2024-08-06 11:54:03

io:format("~4..0B~n", [Num]).

io:format("~4..0B~n", [Num]).

就此别过 2024-08-06 11:54:03

字符串:right(integer_to_list(4), 4, $0)。

string:right(integer_to_list(4), 4, $0).

人间☆小暴躁 2024-08-06 11:54:03

io:format 的问题是,如果你的整数不'不适合,你会得到星号:

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

string:right 的问题是它扔掉了不适合的字符:

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

我还没有找到一个表现如我所期望的库模块(即打印我的数字,即使它不适合填充),所以我编写了自己的格式函数:(

%%------------------------------------------------------------------------------
%% @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].

可以使用 iolist_to_binary/1 来转换结果为二进制,或者您可以使用 lists:flatten(io_lib:format("~s", [结果])) 到将其转换为列表。)

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.)

巡山小妖精 2024-08-06 11:54:03
Eshell V12.0.3  (abort with ^G)
1> F = fun(Max, I)-> case Max - length(integer_to_list(I)) of X when X > 0 -> string:chars($0, X) ++ integer_to_list(I); _ -> I end    end. 
#Fun<erl_eval.43.40011524>
2> F(10, 22).
"0000000022"
3> F(3, 22345).
22345
Eshell V12.0.3  (abort with ^G)
1> F = fun(Max, I)-> case Max - length(integer_to_list(I)) of X when X > 0 -> string:chars($0, X) ++ integer_to_list(I); _ -> I end    end. 
#Fun<erl_eval.43.40011524>
2> F(10, 22).
"0000000022"
3> F(3, 22345).
22345
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文