Convert String obtained from edittext to Integer in Kotlin language
You can use .toInt()
:
val myNumber: Int = "25".toInt()
Note that it throws a NumberFormatException
if the content of the String is not a valid integer.
If you don't like this behavior, you can use .toIntOrNull()
instead (since Kotlin 1.1):
val myNumOrNull: Int? = "25".toIntOrNull()
The above is the general idea but here is a syntax straight out of Android Studio, from a different tutorial I'm doing.
Note that the compiler was perfectly happy to do a cast of a cast.
var myNewInt: Int = myEditTextView.text.toString().toInt()