Decode a String to a Float or Int in Kotlin
This article explores different ways to decode a String to a Float or Int in Kotlin.
1. Using toDouble() function
A simple solution to parse a string to get the corresponding double is using the toDouble() function. A typical invocation for this method would look like:
|
1 2 3 4 5 |
fun main() { val s = "1.1" val d = s.toDouble() println(d) } |
The toDouble() function throws NumberFormatException if the string is not a valid representation of a number. The following code example uses the toDoubleOrNull() function, which parses the string as a Double and returns the result or null if the string is not a valid representation of a number.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val s = "1.1" val i = s.toDoubleOrNull() if (i != null) { println(i) // 1.1 } else { // string cannot be parsed to a double } } |
Alternatively, we can enclose the toDouble() function within a try-catch block.
|
1 2 3 4 5 6 7 8 9 |
fun main() { val s = "1.1f" try { val d = s.toDouble() println(d) } catch (e: NumberFormatException) { // string cannot be parsed to double } } |
To get the corresponding integer value, we can cast the resultant double value using the toInt() function.
|
1 2 3 4 5 6 7 8 9 |
fun main() { val s = "1.1" try { val i = s.toDouble().toInt() println(i) // 1 } catch (e: NumberFormatException) { // string cannot be parsed to double } } |
2. Using toFloat() function
Alternatively, we can use the toFloat() function to parse a string to a float. Or use the toFloatOrNull() function for null-safe version.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val s = "1.1" val i = s.toFloatOrNull() if (i != null) { println(i) // 1.1 } else { // string cannot be parsed to a float } } |
To get the corresponding integer value, cast the resultant float value as discussed before.
|
1 2 3 4 5 6 7 8 9 |
fun main() { val s = "1.1" try { val i = s.toFloat().toInt() println(i) // 1 } catch (e: NumberFormatException) { // string cannot be parsed to float } } |
3. Using toInt() function
To parse the string as a signed decimal integer, we can use the toInt() function. To avoid an exception, we can invoke the toInt() function within a try-catch block, as shown below:
|
1 2 3 4 5 6 7 8 9 |
fun main() { val s = "1" try { val i = s.toInt() println(i) // 1 } catch (e: NumberFormatException) { // string cannot be parsed to an integer } } |
A better solution is to use the toIntOrNull() function, which parses the string as an Int number and returns the result or null if the string is not a valid representation of a number.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val s = "1" val i = s.toIntOrNull() if (i != null) { println(i) // 1 } else { // string cannot be parsed to an integer } } |
That’s all about decoding a String to a Float or Int in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)