How to format a number with padding in Erlang

前端 未结 4 1521
北荒
北荒 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:59

    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.

提交回复
热议问题