I know strings in Erlang can be costly to use. So how do I convert '5'to 5?
Is there anything like io:format('~p',[5]) that would return a formatted string instead of printing to a stream?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The following is probably not the neatest way, but it works:
EDIT: I’ve found that the following function comes in useful:
EDIT 2 (in response to comments): the above function came from a small program I wrote a while back to learn Erlang. I was looking for a string-formatting function and found the behaviour of
io_lib:format/2withinerlcounter-intuitive, for example:At the time, I was unaware of the ‘auto-flattening’ behaviour of output devices mentioned by @archaelus and so concluded that the above behaviour wasn’t what I wanted.
This evening, I went back to this program and replaced calls to the
string_formatfunction above withio_lib:format. The only problems this caused were a few EUnit tests that failed because they were expecting a flattened string. These were easily fixed.I agree with @gleber and @womble that using this function is overkill for converting an integer to a string. If that’s all you need, use
integer_to_list/1. KISS!