[erlang-questions] integer-string conversions.
Sat Apr 9 01:21:07 CEST 2011
# Peter W. Morreale 2011-04-08:
> I bet this has come up a lot, however I can't find anything on the web
> that is appropriate... (Nor am I seeing it in the erlang doc...)
> Need to convert an integer to a string, with a field width and
> precision, such that:
> 2 --> "02"
> ie: right-justified and padded with a leading zeros, depending upon
> field width.
> How do I do this?
17> io_lib:format("~2.10.0B~n", ).
[["0","2"]] %% Flatten me if you have to (which is very rarely).
Personally, I prefer to use a utility function like this:
make_two(N) when N < 10 -> %% 
[$0 | integer_to_list(N)];
just because resulting code is clearer than esoteric formatting syntax IMO.
Similar functions are defined for other field widths that are needed, it's
only a few of them really.
 Yeah, that guard isn't quite exhaustive, you can make it more paranoid
if you like.
More information about the erlang-questions