toLongOrNull
Parses the string to a Long number or returns null
if the string is not a valid representation of a Long.
The string must consist of an optional leading +
or -
sign and decimal digits (0-9
), and fit the valid Short value range (within Long.MIN_VALUE..Long.MAX_VALUE
), otherwise null
is returned.
Since Kotlin
1.1Samples
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
println("0".toLongOrNull()) // 0
println("42".toLongOrNull()) // 42
println("042".toLongOrNull()) // 42
println("-42".toLongOrNull()) // -42
// Long.MAX_VALUE
println("9223372036854775807".toLongOrNull()) // 9223372036854775807
// Long overflow
println("9223372036854775808".toLongOrNull()) // null
// 'a' is not a digit
println("-1a".toLongOrNull()) // null
// underscore
println("1_00".toLongOrNull()) // null
// whitespaces
println(" 22 ".toLongOrNull()) // null
//sampleEnd
}
Parses the string as a Long number and returns the result or null
if the string is not a valid representation of a number.
Since Kotlin
1.1Throws
when radix is not a valid radix for string to number conversion.