[erlang-questions] Stringified integer with leading zeroes

Masklinn masklinn@REDACTED
Wed Jan 25 15:39:38 CET 2012


On 2012-01-25, at 15:30 , Avinash Dhumane wrote:

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

5> io_lib:fwrite("~10..0B", [123]).
[[[48,"000",48,48,48],49,50,51]]
6> lists:flatten(io_lib:fwrite("~10..0B", [123])).
"0000000123"

io_lib:fwrite returns an iolist. In fact you can pass it straight to io:format it's going to print the right thing:

10> io:format(io_lib:fwrite("~10..0B", [123])).    
0000000123ok




More information about the erlang-questions mailing list