mgjohns61585
Convert a string to an integer?
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from a string to an int. Any help would be greatly appreciated.
Most Liked Responses
tyro
Hi there!
To convert a string to an integer you can use String.to_integer/1 e.g.
int = String.to_integer("1")
When it comes to user input you might also want to remove whitespace first which you can do with String.trim/1.
Hope that helps!
benwilson512
Edited for code blocks. I recommend formatting code that you post, it helps others understand what’s going on to help you better.
In your case you wrote :string.to_integer but @tyro wrote String.to_integer. Elixir module names capitalized, erlang modules tend to be lower case. That’s why :rand.uniform uses lower case :rand, it’s an erlang module. String however is an Elixir module, you can find the documentation on it here: String — Elixir v1.20.2









