[erlang-questions] Stringified integer with leading zeroes

Gustav Simonsson <>
Wed Jan 25 15:39:06 CET 2012


Erlang R16B (erts-5.10) [source] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.10  (abort with ^G)
1> lists:flatten(io_lib:format("~5..0B", [123])).
"00123"
2> lists:flatten(io_lib:format("~8..0B", [123])).
"00000123"
3> 

Regards,
Gustav Simonsson

Sent from my PC

----- Original Message -----
From: "Avinash Dhumane" <>
To: "Erlang Questions" <>
Sent: Wednesday, January 25, 2012 3:30:09 PM
Subject: [erlang-questions] Stringified integer with leading zeroes





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> 


_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list