Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an integer to a string in Erlang?

Tags:

string

erlang

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?

like image 377
collapsinghrung Avatar asked Feb 25 '09 21:02

collapsinghrung


People also ask

How do you convert integers to strings?

The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you'll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.

Is string an Erlang?

Strings are lists in erlang (most of the time). The most common reason you would need to do this is a nested lists in a tree where some of the sub-lists are strings which need to be treated as a list entry not a subtree. Without tagging the list operations like flatten and tree traversal become much more difficult.

How do I convert an int to a string in C#?

To convert an integer to string in C#, use the ToString() method.

Can we convert int to string in C# with example?

C# int to string other examplesint val = 4; string msg = "There are " + Convert. ToString(val) + " hawks"; string msg2 = string. Format("There are {0} hawks", val); string msg3 = $"There are {val} hawks"; Console. WriteLine(msg); Console.


2 Answers

There's also integer_to_list/1, which does exactly what you want, without the ugliness.

like image 171
womble Avatar answered Sep 25 '22 06:09

womble


A string is a list:

9> integer_to_list(123).   "123" 
like image 41
Thomas Avatar answered Sep 24 '22 06:09

Thomas