toInt

expect fun String.toInt(): Int(source)

Parses the string to an Int number.

The string must consist of an optional leading + or - sign and decimal digits (0-9), and fit the valid Int value range (within Int.MIN_VALUE..Int.MAX_VALUE), otherwise a NumberFormatException will be thrown.

Since Kotlin

1.0

Throws

if the string is not a valid representation of an Int.

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println("0".toInt()) // 0
println("42".toInt()) // 42
println("042".toInt()) // 42
println("-42".toInt()) // -42
// Int.MAX_VALUE
println("2147483647".toInt()) // 2147483647
// Int overflow
// "2147483648".toInt() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toInt() // will fail with NumberFormatException
// underscore
// "1_000".toInt() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toInt() // will fail with NumberFormatException 
   //sampleEnd
}

expect fun String.toInt(radix: Int): Int(source)

Parses the string as an Int number and returns the result.

Since Kotlin

1.0

Throws

if the string is not a valid representation of a number.

when radix is not a valid radix for string to number conversion.

actual fun String.toInt(): Int(source)

Parses the string to an Int number.

The string must consist of an optional leading + or - sign and decimal digits (0-9), and fit the valid Int value range (within Int.MIN_VALUE..Int.MAX_VALUE), otherwise a NumberFormatException will be thrown.

Since Kotlin

1.1

Throws

if the string is not a valid representation of an Int.

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println("0".toInt()) // 0
println("42".toInt()) // 42
println("042".toInt()) // 42
println("-42".toInt()) // -42
// Int.MAX_VALUE
println("2147483647".toInt()) // 2147483647
// Int overflow
// "2147483648".toInt() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toInt() // will fail with NumberFormatException
// underscore
// "1_000".toInt() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toInt() // will fail with NumberFormatException 
   //sampleEnd
}

actual fun String.toInt(radix: Int): Int(source)

Parses the string as an Int number and returns the result.

Since Kotlin

1.1

Throws

if the string is not a valid representation of a number.

when radix is not a valid radix for string to number conversion.

actual inline fun String.toInt(): Int(source)

Parses the string to an Int number.

The string must consist of an optional leading + or - sign and decimal digits (0-9), and fit the valid Int value range (within Int.MIN_VALUE..Int.MAX_VALUE), otherwise a NumberFormatException will be thrown.

Since Kotlin

1.0

Throws

if the string is not a valid representation of an Int.

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println("0".toInt()) // 0
println("42".toInt()) // 42
println("042".toInt()) // 42
println("-42".toInt()) // -42
// Int.MAX_VALUE
println("2147483647".toInt()) // 2147483647
// Int overflow
// "2147483648".toInt() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toInt() // will fail with NumberFormatException
// underscore
// "1_000".toInt() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toInt() // will fail with NumberFormatException 
   //sampleEnd
}

actual inline fun String.toInt(radix: Int): Int(source)

Parses the string as an Int number and returns the result.

Since Kotlin

1.1

Throws

if the string is not a valid representation of a number.

when radix is not a valid radix for string to number conversion.

actual inline fun String.toInt(): Int(source)

Parses the string to an Int number.

The string must consist of an optional leading + or - sign and decimal digits (0-9), and fit the valid Int value range (within Int.MIN_VALUE..Int.MAX_VALUE), otherwise a NumberFormatException will be thrown.

Since Kotlin

1.3

Throws

if the string is not a valid representation of an Int.

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println("0".toInt()) // 0
println("42".toInt()) // 42
println("042".toInt()) // 42
println("-42".toInt()) // -42
// Int.MAX_VALUE
println("2147483647".toInt()) // 2147483647
// Int overflow
// "2147483648".toInt() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toInt() // will fail with NumberFormatException
// underscore
// "1_000".toInt() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toInt() // will fail with NumberFormatException 
   //sampleEnd
}

actual inline fun String.toInt(radix: Int): Int(source)

Parses the string as an Int number and returns the result.

Since Kotlin

1.3

Throws

if the string is not a valid representation of a number.

when radix is not a valid radix for string to number conversion.

actual fun String.toInt(): Int(source)

Parses the string to an Int number.

The string must consist of an optional leading + or - sign and decimal digits (0-9), and fit the valid Int value range (within Int.MIN_VALUE..Int.MAX_VALUE), otherwise a NumberFormatException will be thrown.

Since Kotlin

1.8

Throws

if the string is not a valid representation of an Int.

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println("0".toInt()) // 0
println("42".toInt()) // 42
println("042".toInt()) // 42
println("-42".toInt()) // -42
// Int.MAX_VALUE
println("2147483647".toInt()) // 2147483647
// Int overflow
// "2147483648".toInt() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toInt() // will fail with NumberFormatException
// underscore
// "1_000".toInt() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toInt() // will fail with NumberFormatException 
   //sampleEnd
}

actual fun String.toInt(radix: Int): Int(source)

Parses the string as an Int number and returns the result.

Since Kotlin

1.8

Throws

if the string is not a valid representation of a number.

when radix is not a valid radix for string to number conversion.

actual fun String.toInt(): Int(source)

Parses the string to an Int number.

The string must consist of an optional leading + or - sign and decimal digits (0-9), and fit the valid Int value range (within Int.MIN_VALUE..Int.MAX_VALUE), otherwise a NumberFormatException will be thrown.

Since Kotlin

1.8

Throws

if the string is not a valid representation of an Int.

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println("0".toInt()) // 0
println("42".toInt()) // 42
println("042".toInt()) // 42
println("-42".toInt()) // -42
// Int.MAX_VALUE
println("2147483647".toInt()) // 2147483647
// Int overflow
// "2147483648".toInt() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toInt() // will fail with NumberFormatException
// underscore
// "1_000".toInt() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toInt() // will fail with NumberFormatException 
   //sampleEnd
}

actual fun String.toInt(radix: Int): Int(source)

Parses the string as an Int number and returns the result.

Since Kotlin

1.8

Throws

if the string is not a valid representation of a number.

when radix is not a valid radix for string to number conversion.