toInt
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.0Throws
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
}
Parses the string as an Int number and returns the result.
Since Kotlin
1.0Throws
if the string is not a valid representation of a number.
when radix is not a valid radix for string to number conversion.
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.1Throws
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
}
Parses the string as an Int number and returns the result.
Since Kotlin
1.1Throws
if the string is not a valid representation of a number.
when radix is not a valid radix for string to number conversion.
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.0Throws
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
}
Parses the string as an Int number and returns the result.
Since Kotlin
1.1Throws
if the string is not a valid representation of a number.
when radix is not a valid radix for string to number conversion.
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.3Throws
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
}
Parses the string as an Int number and returns the result.
Since Kotlin
1.3Throws
if the string is not a valid representation of a number.
when radix is not a valid radix for string to number conversion.
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.8Throws
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
}
Parses the string as an Int number and returns the result.
Since Kotlin
1.8Throws
if the string is not a valid representation of a number.
when radix is not a valid radix for string to number conversion.
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.8Throws
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
}
Parses the string as an Int number and returns the result.
Since Kotlin
1.8Throws
if the string is not a valid representation of a number.
when radix is not a valid radix for string to number conversion.