[erlang-questions] Stringified integer with leading zeroes

Bengt Kleberg bengt.kleberg@REDACTED
Wed Jan 25 15:37:57 CET 2012


Greetings,

Please try lists:flatten/1 on the final example
(io_lib:fwrite("~10.10.0s", ["123"]).).


bengt

 On Wed, 2012-01-25 at 15:30 +0100, 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.
> 
> 
> $ 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>
> 
> 




More information about the erlang-questions mailing list