如何在 Erlang 中使用填充格式化数字 [英] How to format a number with padding in Erlang
本文介绍了如何在 Erlang 中使用填充格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将一个整数的输出填充到给定的长度.
I need to pad the output of an integer to a given length.
例如,长度为 4 位,整数 4 的输出是0004"而不是4".如何在 Erlang 中做到这一点?
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?
推荐答案
对 Zed 的回答加一点解释:
adding a bit of explanation to Zed's answer:
Erlang 格式规范是:~FPPadModC.
Erlang Format specification is: ~F.P.PadModC.
"~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
是换行符.
这篇关于如何在 Erlang 中使用填充格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文