How to convert String to Long in Kotlin?

1. string.toLong()

Parses the string as a [Long] number and returns the result.

@throws NumberFormatException if the string is not a valid
representation of a number.

2. string.toLongOrNull()

Parses the string as a [Long] number and returns the result or null
if the string is not a valid representation of a number.

3. str.toLong(10)

Parses the string as a [Long] number and returns the result.

@throws NumberFormatException if the string is not a valid
representation of a number.

@throws IllegalArgumentException when
[radix] is not a valid radix for string to number conversion.

public inline fun String.toLong(radix: Int): Long = java.lang.Long.parseLong(this, checkRadix(radix))

4. string.toLongOrNull(10)

Parses the string as a [Long] number and returns the result or null
if the string is not a valid representation of a number.

@throws IllegalArgumentException when [radix] is not a valid radix for string
to number conversion.

public fun String.toLongOrNull(radix: Int): Long? {...}

5. java.lang.Long.valueOf(string)

public static Long valueOf(String s) throws NumberFormatException

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)