[erlang-questions] Stringified integer with leading zeroes

Avinash Dhumane avinash@REDACTED
Wed Jan 25 15:30:09 CET 2012


How to get an integer 123 as 10-char long string "0000000123" with leading zeroes, i.e. erlang term [48, 48, 48, 48, 48, 48, 48, 49, 50, 51]?

Following is not what I expect! I tried with both ~B and ~s.

$ erl

Erlang R14B02 (erts-5.8.3) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.8.3  (abort with ^G)
1> io_lib:fwrite("~B", [123]).
["123"]
2> io_lib:fwrite("~s", ["123"]).
["123"]
3> io_lib:fwrite("~5B", [123]).
[["  ",49,50,51]]
4> io_lib:fwrite("~5..0B", [123]).
[["00",49,50,51]]
5> io_lib:fwrite("~5..0s", ["123"]).
[["00",49,50,51]]
6> io_lib:fwrite("~5.5.0s", ["123"]).
[["00",49,50,51]]
7> io_lib:fwrite("~8..0B", [123]).
[[[48,"00",48,48],49,50,51]]
8> io_lib:fwrite("~8..0s", ["123"]).
[[[48,"00",48,48],49,50,51]]
9> io_lib:fwrite("~8.8.0s", ["123"]).
[[[48,"00",48,48],49,50,51]]
10> io_lib:fwrite("~10..0B", [123]).
[[[48,"000",48,48,48],49,50,51]]
11> io_lib:fwrite("~10.10.0s", ["123"]).
[[[48,"000",48,48,48],49,50,51]]
12>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120125/0ba50918/attachment.htm>


More information about the erlang-questions mailing list