This may not be the easiest way, but it works:
1> lists:flatten(io_lib:format("~p", [35365])). "35365"
EDIT: I found that a useful feature:
%% string_format/2 %% Like io:format except it returns the evaluated string rather than write %% it to standard output. %% Parameters: %% 1. format string similar to that used by io:format. %% 2. list of values to supply to format string. %% Returns: %% Formatted string. string_format(Pattern, Values) -> lists:flatten(io_lib:format(Pattern, Values)).
EDIT 2 (in response to comments): The above function came from a small program that I wrote some time ago to learn Erlang. I was looking for a string formatting function and found the io_lib:format/2 behavior inside erl counter-intuitive, for example:
1> io_lib:format("2 + 2 = ~p", [2+2]). [50,32,43,32,50,32,61,32,"4"]
At that time, I did not know about the "automatic alignment" of the behavior of the output devices mentioned by @archaelus, and therefore came to the conclusion that the above behavior was not what I wanted.
This evening I returned to this program and replaced the calls with the string_format function above using io_lib:format . The only problems that caused this were a few EUnit tests that failed because they were expecting a flattened string. They were easily fixed.
I agree with @gleber and @womble that using this function is redundant to convert an integer to a string. If that's all you need, use integer_to_list/1 . KISS!
Luke Woodward Feb 25 '09 at 21:48 2009-02-25 21:48
source share