如何使用Erlang中的填充格式化数字 [英] How to format a number with padding in Erlang

查看:117
本文介绍了如何使用Erlang中的填充格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,长度为4位数的整数4的输出为 0004而不是4。如何在Erlang中执行此操作?

解决方案

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



Erlang格式规范是:〜FP PadModC。



〜4..0B〜n转换为:

 〜F。 =〜4。 (字段宽度为4)
P. =。 (无精度指定)
Pad = 0(带零的Pad)
Mod =(无控制序列修饰符指定)
C = B(控制序列B =默认基数10中的整数)

〜n 是新行。 >

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?

解决方案

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.

这篇关于如何使用Erlang中的填充格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆