Convert a string to number in Kotlin :
In this post, I will show you different ways to convert a string to number in Kotlin. This problem has a lot of use cases like your application is getting string values from the server and you want to convert it to number safely before processing. Or you want to convert the user input value to number before sending to the server. Let’s check the programs :
Convert to integer using toIntOrNull :
Thanks to Kotlin, we have one new method that makes this task easier for us. This is called toIntOrNull and below are its definitions :
fun String.toIntOrNull(): Int?
and :
fun String.toIntOrNull(radix: Int): Int?
The second one throws one IllegalArgumentException if the radix is not valid.
Let’s take a look at the below example :
fun main() {
val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f")
strArray.forEach {
println("$it => ${it.toIntOrNull()}")
}
}
Here, we are creating one array of strings, we are iterating through the string elements one by one and getting the integer value using toIntOrNull. It will print the below output :
10 => 10
10.4 => null
0 => 0
0.0 => null
-0 => 0
-0.0 => null
-3 => -3
-298889 => -298889
hello => null
1234567 => 1234567
AEAAAA => null
12.3f => null
This is the easiest and the safest method to convert one string to number in Kotlin.
Using toInt() :
toInt() method converts the string to an integer. But if the string is not a valid integer, it throws one NumberFormatException. So, if you want to use this method, always make sure to wrap it inside a try-catch block.
It has two variants :
String.toInt()
String.toInt(radix)
The radix version throws one IllegalArgumentException if it is not a valid radix.
Example :
fun main() {
val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f")
strArray.forEach {
try {
println("$it => ${it.toInt()}")
} catch (e: NumberFormatException) {
println("$it => null")
}
}
}
It will print :
10 => 10
10.4 => null
0 => 0
0.0 => null
-0 => 0
-0.0 => null
-3 => -3
-298889 => -298889
hello => null
1234567 => 1234567
AEAAAA => null
12.3f => null
Convert string to long, float and double :
Similar to integer, we have other methods to convert a string to long, float and double safely :
toLong()
toLongOrNull()
toFloat()
toFloatOrNull()
toDouble()
toDoubleOrNull()
Let’s write the above program using all these methods. I am using only OrNull versions below :
fun main() {
val strArray =
arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f")
strArray.forEach {
println("$it => Int: ${it.toIntOrNull()}, Long: ${it.toLongOrNull()}, Float: ${it.toFloatOrNull()}, Double: ${it.toDoubleOrNull()}")
}
}
It will print :
10 => Int: 10, Long: 10, Float: 10.0, Double: 10.0
10.4 => Int: null, Long: null, Float: 10.4, Double: 10.4
0 => Int: 0, Long: 0, Float: 0.0, Double: 0.0
0.0 => Int: null, Long: null, Float: 0.0, Double: 0.0
-0 => Int: 0, Long: 0, Float: -0.0, Double: -0.0
-0.0 => Int: null, Long: null, Float: -0.0, Double: -0.0
-3 => Int: -3, Long: -3, Float: -3.0, Double: -3.0
-298889 => Int: -298889, Long: -298889, Float: -298889.0, Double: -298889.0
hello => Int: null, Long: null, Float: null, Double: null
1234567 => Int: 1234567, Long: 1234567, Float: 1234567.0, Double: 1234567.0
AEAAAA => Int: null, Long: null, Float: null, Double: null
12.3f => Int: null, Long: null, Float: 12.3, Double: 12.3
Example of using radix :
fun main() {
val strArray = arrayOf("FFFF","AEFF")
strArray.forEach {
println("$it => Int: ${it.toIntOrNull(16)}, Long: ${it.toLongOrNull(16)}")
}
}
Output :
FFFF => Int: 65535, Long: 65535
AEFF => Int: 44799, Long: 44799