Formatting integers with io_lib

Bryan Robinson [ICG-MKTS] brobinson@REDACTED
Wed Oct 27 21:32:47 CEST 2010


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


More information about the erlang-questions mailing list