[erlang-questions] Formatting integers with io_lib

Ulf Wiger <>
Wed Oct 27 21:41:13 CEST 2010


1> {H,M,S} = time().
{21,37,1}
2> io:fwrite("~2.2.0w.~2.2.0w.~2.2.0w~n",[H,M,S]).
21.37.01

BR,
Ulf W

On 27 Oct 2010, at 21:32, Bryan Robinson [ICG-MKTS] wrote:

> I may have missed or misunderstood something in the io_lib and io
> library documentation, but is there a way to use io_lib:format() to
> insert leading zero characters on small integers?  For example, in the
> Erlang shell:
> 
> {_,Time} = calendar:local_time().
> {H,M,S} = Time.
> Msg = io_lib:format("~w:~w:~w", [H,M,S]").
> io_format("~s", Msg).
> 
> I get "15:2:9".  I would like to get back "15:02:09", and of course when
> the integer is two digits I would not like the leading zero.
> 
> Is there a similar way to do this like with the C stdlib and the %0n
> where n=2 formatter?
> 
> printf("%02d:%02d:%02d", h, m, t);
> 
> I would appreciate any pointers or tips.  Thank you!
> 
> -- 
> 
> - Bryan
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
> 

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com





More information about the erlang-questions mailing list