How to Convert String to Int, Float, Double, Long in Kotlin

Last updated on: by Digamber
This tutorial explains some of the useful methods about how to convert String to Int, Float, Double, Long in Kotlin/Android.

Let’s get started.

Kotlin Convert String to Int Example

We are going to test the following examples:

The toInt() method helps to parse the string to an Int numerical value. However, It throws a NumberFormatException exception if it finds the string is not a correct representation of a number value.

The toIntOrNull() method parses the string to an Int and returns null if it finds string doesn’t belong valid numerical representation.

fun main() {
   var str:String = "20"
   var num:Int = str.toInt()
   print(num) 
   // output: 20
    
    
   val strA = "30.1"
   val numA: Int? = strA.toInt()
   println(strA) 
   // output: Exception in thread "main" java.lang.NumberFormatException: For input string: "30.1"
 
   val int: Int? = strA.toIntOrNull()
   println(int)
   // output: null
    
   val strB = "555"
   val intB: Int? = strB.toInt(6)
   println(intB)
   // output: 215       
}

Convert String to Float in Kotlin/Android

Here are the example that we are about to explore:

The toFloat() method converts the string to a Float, It throws the NumberFormatException exception when the string is not a legitimate representation of a Float.

The toFloatOrNull() method parses the string to a Float, It returns a null value when it finds the string is not a valid representation of a Float.

fun main() {
  val floatStr1 = "234.12f";
  val floatVal1: Float? = floatStr1.toFloat()
  println(floatStr1)
  // output: 234.12f
	
  
  val floatStr2 = "C234.345";
  val floatVal2: Float? = floatStr2.toFloat()
  // output: Exception in thread "main" java.lang.NumberFormatException: For input string: "C234.345"
      
  val floatStr3 = "A23.456";    
  val floatStr4: Float? = floatStr3.toFloatOrNull()
  println(floatStr4)
  // output: null
}

Convert String to Double in Kotlin

We will explore the following examples:

The toDouble() method converts the string to a Double, It returns NumberFormatException if it sees the string is not a valid representation of a Double.

The toDoubleOrNull() method parses the string to a Double, It returns a null value if it finds the string is not a valid representation of a Double.

fun main() {
  val doubleStr1 = "2.123456789"
  val doubleVal1: Double? = doubleStr1.toDouble()
  println(doubleVal1)
  // output: 2.123456789
  
	
  val doubleStr2 = "C2.12";
  val doubleVal2: Double? = doubleStr2.toDouble()
  println(doubleVal2)
  // output: Exception in thread "main" java.lang.NumberFormatException: For input string: "C2.12"
	
  val doubleStr3 = "X1.007";
  val doubleVal3: Double? = doubleStr3.toDoubleOrNull()
  println(doubleVal3)
  // output: null
}

Convert String to Long in Kotlin

We are about to understand the following Kotlin methods with examples:

The toLong() method parses the string to a Long, and It returns a NumberFormatException when it finds the string is not a valid representation of a Long.

The toLongOrNull() method converts the string to a Long, It returns a null value when it finds the string is not a valid representation of a Long.

The toLong(10) method parses the string as a “Long === 10” number and returns the result. It throws NumberFormatException if it sees string is not a valid representation of a number.

It even throws an IllegalArgumentException when it finds the “radix” is not a valid radix for the string to number conversion.

fun main() {
  val longStr1 = "31000000"
  val longVal1: Long? = longStr1.toLong()
  println(longVal1)
  // output: 31000000
	
    
  val longStr2 = "21.21";
  val longVal2: Long? = longStr2.toLong()
  // output: Exception in thread "main" java.lang.NumberFormatException: For input string: "21.21"
	
  val longStr3 = "11.11";    
  val longVal3: Long? = longStr3.toLongOrNull()
  println(longVal3)
  // output: null
	
    
  val longStr4 = "AAA"
  val longVal4: Long? = longStr4.toLong(14)
  println(longVal4)
  // output: 2110
  
  val longStr5 = "AAA"
  val longVal5: Long? = longStr5.toLong(8)
  println(longVal5)
  // output: Exception in thread "main" java.lang.NumberFormatException: For input string: "AAA"
	
    
  val longStr6 = ""
  val longVal6: Long? = longStr6.toLongOrNull(8)
  println(longVal6)
  // output: null
}

Conclusion

So, that was it. In this tutorial, we have come across the prevalent programming concepts converting String to a Number. We have learned how to work with Kotlin String type conversion methods. On top of that, we successfully converted String to Int, Float, Double, Long in Kotlin/Android.

We barely scratched the surface; however, if you want to dig deep, then you should check out the Types in Kotlin and Kotlin API.

Happy Coding!

Reference: Bezkoder

positronX.io - Tamso Ma Jyotirgamaya
Digamber

A Full-stack developer with a passion to solve real world problems through functional programming.