Type Conversion in Kotlin: toInt, toDouble, toString and More

Kotlin never converts one number type to another on its own. If you have an Int and need a Long or a Double, you convert it explicitly. This avoids hidden bugs, but it means you need to know the conversion functions.

Why Kotlin doesn’t convert automatically

val count: Int = 10
val total: Long = count        // ❌ Type mismatch
val total2: Long = count.toLong() // ✅

Converting between number types

Every number type has these functions: toByte(), toShort(), toInt(), toLong(), toFloat() and toDouble().

fun main() {
    val items = 7
    val people = 2

    println(items / people)                 // Int division
    println(items.toDouble() / people)      // Double division

    val price = 99.99
    println(price.toInt())                  // decimal part is cut off, not rounded
}

Output:

3
3.5
99

toInt() on a Double cuts off the decimals (99.99 becomes 99). To round, use roundToInt() from kotlin.math:

import kotlin.math.roundToInt

fun main() {
    println(4.4.roundToInt())
    println(4.5.roundToInt())
    println((-4.5).roundToInt())
}

Output:

4
5
-4

String to number

Use toInt(), toLong() or toDouble() on a string:

fun main() {
    val ageText = "25"
    val age = ageText.toInt()
    println(age + 5)

    val priceText = "19.99"
    println(priceText.toDouble() * 2)
}

Output:

30
39.98

If the text isn’t a valid number, toInt() throws a NumberFormatException and the program crashes. When the text comes from a user, use the OrNull versions instead. They return null rather than crashing:

fun main() {
    println("42".toIntOrNull())
    println("abc".toIntOrNull())
    println("12.5".toIntOrNull())   // not a whole number

    val input = "abc"
    val number = input.toIntOrNull() ?: 0   // use 0 if it's not a number
    println(number)
}

Output:

42
null
null
0

The ?: (Elvis operator) gives a default value when the left side is null. You’ll learn more about it in the null safety lesson.

Number to String

fun main() {
    val score = 95
    val text = score.toString()
    println(text + "%")

    val pi = 3.14159
    println("%.2f".format(pi))   // 2 decimal places
    println("Score: $score")     // string template, no conversion needed
}

Output:

95%
3.14
Score: 95

Char and Int

fun main() {
    val digit = '7'
    println(digit.digitToInt())  // the digit's value
    println(digit.code)          // the Unicode code of the character

    println(65.toChar())         // code to Char
    println('A' + 2)             // Char arithmetic
}

Output:

7
55
A
C

'7'.code is 55, not 7, because it gives the character’s Unicode code. Use digitToInt() to get the digit’s value.

String to Boolean

fun main() {
    println("true".toBoolean())
    println("TRUE".toBoolean())
    println("yes".toBoolean())
    println("yes".toBooleanStrictOrNull())
}

Output:

true
true
false
null

Quick reference

From → ToFunction
Int → DoubletoDouble()
Double → Int (cut off)toInt()
Double → Int (rounded)roundToInt()
String → InttoInt() or toIntOrNull()
String → DoubletoDouble() or toDoubleOrNull()
Any → StringtoString() or a string template
Char digit → IntdigitToInt()
Int → ChartoChar()

Common mistakes

  • Crashing on user input: readln().toInt() crashes on “abc”. Use toIntOrNull() and handle null.
  • Expecting toInt() to round: 2.99.toInt() is 2.
  • Using .code for a digit: use digitToInt().

Exercise

A user types their marks as text: “78”, “91” and “x”. Convert each one, skip the invalid value and print the average.

fun main() {
    val inputs = listOf("78", "91", "x")
    val marks = inputs.mapNotNull { it.toIntOrNull() }
    println("Valid marks: $marks")
    println("Average: ${marks.average()}")
}

Output:

Valid marks: [78, 91]
Average: 84.5

FAQ

How do I convert a String to an Int in Kotlin?

Use "42".toInt(), or toIntOrNull() if the text might not be a number.

Why does Kotlin not convert Int to Long automatically?

To prevent hidden bugs and precision loss. Every conversion is visible in your code.

How do I round a Double to an Int?

Use roundToInt() from kotlin.math. toInt() just drops the decimals.

WhatsApp us📞 Call now