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?
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.
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.
To convert an integer to string in C#, use the ToString() method.
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.
There's also integer_to_list/1
, which does exactly what you want, without the ugliness.
A string is a list:
9> integer_to_list(123). "123"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With