toLong
Parses the string to a Long number.
The string must consist of an optional leading +
or -
sign and decimal digits (0-9
), and fit the valid Long value range (within Long.MIN_VALUE..Long.MAX_VALUE
), otherwise a NumberFormatException will be thrown.
Since Kotlin
1.0Throws
if the string is not a valid representation of a Long.
Samples
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
println("0".toLong()) // 0
println("42".toLong()) // 42
println("042".toLong()) // 42
println("-42".toLong()) // -42
// Long.MAX_VALUE
println("9223372036854775807".toLong()) // 9223372036854775807
// Long overflow
// "9223372036854775808".toLong() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toLong() // will fail with NumberFormatException
// underscore
// "1_000".toLong() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toLong() // will fail with NumberFormatException
//sampleEnd
}
Parses the string as a Long 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 a Long number.
The string must consist of an optional leading +
or -
sign and decimal digits (0-9
), and fit the valid Long value range (within Long.MIN_VALUE..Long.MAX_VALUE
), otherwise a NumberFormatException will be thrown.
Since Kotlin
1.1Throws
if the string is not a valid representation of a Long.
Samples
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
println("0".toLong()) // 0
println("42".toLong()) // 42
println("042".toLong()) // 42
println("-42".toLong()) // -42
// Long.MAX_VALUE
println("9223372036854775807".toLong()) // 9223372036854775807
// Long overflow
// "9223372036854775808".toLong() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toLong() // will fail with NumberFormatException
// underscore
// "1_000".toLong() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toLong() // will fail with NumberFormatException
//sampleEnd
}
Parses the string as a Long 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 a Long number.
The string must consist of an optional leading +
or -
sign and decimal digits (0-9
), and fit the valid Long value range (within Long.MIN_VALUE..Long.MAX_VALUE
), otherwise a NumberFormatException will be thrown.
Since Kotlin
1.0Throws
if the string is not a valid representation of a Long.
Samples
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
println("0".toLong()) // 0
println("42".toLong()) // 42
println("042".toLong()) // 42
println("-42".toLong()) // -42
// Long.MAX_VALUE
println("9223372036854775807".toLong()) // 9223372036854775807
// Long overflow
// "9223372036854775808".toLong() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toLong() // will fail with NumberFormatException
// underscore
// "1_000".toLong() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toLong() // will fail with NumberFormatException
//sampleEnd
}
Parses the string as a Long 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 a Long number.
The string must consist of an optional leading +
or -
sign and decimal digits (0-9
), and fit the valid Long value range (within Long.MIN_VALUE..Long.MAX_VALUE
), otherwise a NumberFormatException will be thrown.
Since Kotlin
1.3Throws
if the string is not a valid representation of a Long.
Samples
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
println("0".toLong()) // 0
println("42".toLong()) // 42
println("042".toLong()) // 42
println("-42".toLong()) // -42
// Long.MAX_VALUE
println("9223372036854775807".toLong()) // 9223372036854775807
// Long overflow
// "9223372036854775808".toLong() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toLong() // will fail with NumberFormatException
// underscore
// "1_000".toLong() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toLong() // will fail with NumberFormatException
//sampleEnd
}
Parses the string as a Long 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 a Long number.
The string must consist of an optional leading +
or -
sign and decimal digits (0-9
), and fit the valid Long value range (within Long.MIN_VALUE..Long.MAX_VALUE
), otherwise a NumberFormatException will be thrown.
Since Kotlin
1.8Throws
if the string is not a valid representation of a Long.
Samples
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
println("0".toLong()) // 0
println("42".toLong()) // 42
println("042".toLong()) // 42
println("-42".toLong()) // -42
// Long.MAX_VALUE
println("9223372036854775807".toLong()) // 9223372036854775807
// Long overflow
// "9223372036854775808".toLong() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toLong() // will fail with NumberFormatException
// underscore
// "1_000".toLong() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toLong() // will fail with NumberFormatException
//sampleEnd
}
Parses the string as a Long 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 a Long number.
The string must consist of an optional leading +
or -
sign and decimal digits (0-9
), and fit the valid Long value range (within Long.MIN_VALUE..Long.MAX_VALUE
), otherwise a NumberFormatException will be thrown.
Since Kotlin
1.8Throws
if the string is not a valid representation of a Long.
Samples
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
println("0".toLong()) // 0
println("42".toLong()) // 42
println("042".toLong()) // 42
println("-42".toLong()) // -42
// Long.MAX_VALUE
println("9223372036854775807".toLong()) // 9223372036854775807
// Long overflow
// "9223372036854775808".toLong() // will fail with NumberFormatException
// 'a' is not a digit
// "-1a".toLong() // will fail with NumberFormatException
// underscore
// "1_000".toLong() // will fail with NumberFormatException
// whitespaces
// " 1000 ".toLong() // will fail with NumberFormatException
//sampleEnd
}
Parses the string as a Long 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.