toLong

expect fun String.toLong(): Long(source)

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.0

Throws

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
}

expect fun String.toLong(radix: Int): Long(source)

Parses the string as a Long 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.toLong(): Long(source)

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.1

Throws

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
}

actual fun String.toLong(radix: Int): Long(source)

Parses the string as a Long 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.toLong(): Long(source)

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.0

Throws

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
}

actual inline fun String.toLong(radix: Int): Long(source)

Parses the string as a Long 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.toLong(): Long(source)

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.3

Throws

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
}

actual inline fun String.toLong(radix: Int): Long(source)

Parses the string as a Long 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.toLong(): Long(source)

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.8

Throws

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
}

actual fun String.toLong(radix: Int): Long(source)

Parses the string as a Long 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.toLong(): Long(source)

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.8

Throws

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
}

actual fun String.toLong(radix: Int): Long(source)

Parses the string as a Long 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.