kotlin-convert-string-to-int-double-feature-image

Kotlin/Android – Convert String to Int, Long, Float, Double

In this tutorial, I will show you how to convert String to Int, Long, Float, Double in Kotlin/Android.

Kotlin convert String to Int

The example uses:

  • toInt() to parse the string to an Int, NumberFormatException is thrown if the string is not a valid representation of an Integer.
  • toIntOrNull() to convert the string to an Int, return a null if the string is not a valid representation of an Integer.

By default, the radix is 10. You can use any valid radix by passing a parameter to the methods above.

package com.bezkoder.kotlin.numberparser

fun main(args: Array) {

  val istr1 = "42"
  val int1: Int? = istr1.toInt()
  println(int1)
  // 42

  val istr2 = "42.1"
  val int2: Int? = istr2.toInt()
  // Exception in thread "main" java.lang.NumberFormatException: For input string: "42.1"

  val int2: Int? = istr2.toIntOrNull()
  println(int2)
  // null

  val istr3 = "1111"
  val int3: Int? = istr3.toInt(2)
  println(int3)
  // 15

  val istr4 = "1234"
  val int4: Int? = istr4.toInt(2)
  // Exception in thread "main" java.lang.NumberFormatException: For input string: "1234"
	
  val int4: Int? = istr4.toIntOrNull(2)
  println(int4)
  // null
}

Kotlin convert String to Long

The example shows you how to use:

  • toLong() to parse the string to a Long, NumberFormatException is thrown if the string is not a valid representation of a Long.
  • toLongOrNull() to convert the string to a Long, return a null if the string is not a valid representation of a Long.

The radix is 10 by default. To change to another valid radix, you can pass a parameter to these methods.

package com.bezkoder.kotlin.numberparser

fun main(args: Array) {

  val lstr1 = "4200000000"
  val long1: Long? = lstr1.toLong()
  println(long1)
  // 4200000000
	
  val lstr2 = "42.42";
  val long2: Long? = lstr2.toLong()
  // Exception in thread "main" java.lang.NumberFormatException: For input string: "42.42"
	
  val long2: Long? = lstr2.toLongOrNull()
  println(long2)
  // null
	
  val lstr3 = "FFFF"
  val long3: Long? = lstr3.toLong(16)
  println(long3)
  // 65535

  val lstr4 = "FF"
  val long4: Long? = lstr4.toLong(8)
  // Exception in thread "main" java.lang.NumberFormatException: For input string: "1234"
	
  val long4: Long? = lstr4.toLongOrNull(8)
  println(long4)
  // null
}

Kotlin convert String to Float

In the following example, we will use:

  • toFloat() to parse the string to a Float, NumberFormatException is thrown if the string is not a valid representation of a Float.
  • toFloatOrNull() to convert the string to a Float, return a null if the string is not a valid representation of a Float.
package com.bezkoder.kotlin.numberparser

fun main(args: Array) {

  val fstr1 = "123.45f"
  val float1: Float? = fstr1.toFloat()
  println(float1)
  // 123.45
	
  val fstr2 = "A123.456";
  val float2: Float? = fstr2.toFloat()
  // Exception in thread "main" java.lang.NumberFormatException: For input string: "A123.456"
	
  val float2: Float? = fstr2.toFloatOrNull()
  println(float2)
  // null
}

Kotlin convert String to Double

The example shows you way to use:

  • toDouble() to parse the string to a Double, NumberFormatException is thrown if the string is not a valid representation of a Double.
  • toDoubleOrNull() to convert the string to a Double, return a null if the string is not a valid representation of a Double.
package com.bezkoder.kotlin.numberparser

fun main(args: Array) {

  val dstr1 = "1.234567899999"
  val double1: Double? = dstr1.toDouble()
  println(double1)
  // 1.234567899999
	
  val dstr2 = "A1.23";
  val double2: Double? = dstr2.toDouble()
  // Exception in thread "main" java.lang.NumberFormatException: For input string: "A1.23"
	
  val double2: Double? = dstr2.toDoubleOrNull()
  println(double2)
  // null
}

Conclusion

One of the most common tasks in any programming language is to convert a String to a Number type. Today we’ve known way to convert a String to some types such as: Int, Long, Float, Double in Kotlin/Android.

Happy learning! See you again.

Further Reading

Leave a Reply

Your email address will not be published. Required fields are marked *