Kotlin Convert String to Long

In the tutorial, JavaSampleApproach will guide you how to convert Kotlin String to Long.

Related posts:
Kotlin Convert String to Int


Working environment:
– Java 8
– Kotlin 1.1.61

I. Kotlin toLong() method

1 String.toLong(): Long

– use method signature: public inline fun String.toLong(): Long

package com.javasampleapproach.string2long
fun main(args : Array<String>) {
	// use method:
	// -> public inline fun String.toLong(): Long = java.lang.Long.parseLong(this)
	val number: Long = "123".toLong();
	println(number) // 123
	
	// if the string is not a valid representation of a number
	// -> throw NumberFormatException
	try{
		"12w".toLong();
	}catch(e: NumberFormatException){
		println(e.printStackTrace())
		// -> print on console:
		/*
			java.lang.NumberFormatException: For input string: "12w"
			at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
			at java.lang.Long.parseLong(Long.java:589)
			at java.lang.Long.parseLong(Long.java:631)
			at com.javasampleapproach.string2long.ConvertString2LongKt.main(ConvertString2Long.kt:12)
		*/
	}
}

Strig.toLong() will throw a NumberFormatException if the string is not a valid representation of a number.
String.toLong() just uses Integer.parseLong of Java for converting
-> detail: public inline fun String.toLong(): Long = java.lang.Long.parseLong(this)

2 String.toLong(radix: Int): Long

If we want to work with radix, we can use another method signature toLong(radix: Int)
-> detail: public inline fun String.toLong(radix: Int): Long = java.lang.Long.parseLong(this, checkRadix(radix))

package com.javasampleapproach.string2long

fun main(args : Array<String>) {
	// use method:
	// -> public inline fun String.toLong(radix: Int): Long = java.lang.Long.parseLong(this, checkRadix(radix))
	val numberWithRadix2: Long = "101".toLong(2)
	println("numberWithRadix2 = $numberWithRadix2") // numberWithRadix2 = 5
	
	// if the string is not a valid representation of a number
	// -> throw NumberFormatException
	try{
		"123".toLong(2);
	}catch(e: NumberFormatException){
		println(e.printStackTrace())
		// -> print on console:
		/*
			java.lang.NumberFormatException: For input string: "123"
			at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
			at java.lang.Long.parseLong(Long.java:589)
			at com.javasampleapproach.string2long.ConvertString2LongKt.main(ConvertString2Long.kt:12)
 		*/
	}
	
	// if radix is not a valid radix
	// -> throw IllegalArgumentException
	
	try{
		"101".toLong(-1)
	}catch(e: IllegalArgumentException){
		println(e.printStackTrace())
		// -> print on console:
		/*
			java.lang.IllegalArgumentException: radix -1 was not in valid range 2..36
			at kotlin.text.CharsKt__CharJVMKt.checkRadix(CharJVM.kt:155)
			at com.javasampleapproach.string2long.ConvertString2LongKt.main(ConvertString2Long.kt:28)
		*/
	}
}

Exception:

  • String.toLong(radix: Int) will throw a NumberFormatException if the string is not a valid representation of a number.
  • String.toLong(radix: Int) will throw a IllegalArgumentException if the radix is not a valid radix.
  • String.toLong(radix: Int) just uses Long.parseLong(this, checkRadix(radix)) of Java for converting,
    -> detail: public inline fun String.toLong(radix: Int): Long = java.lang.Long.parseLong(this, checkRadix(radix))

    II. Kotlin toLongOrNull() method

    1. String.toLongOrNull(): Long?

    Method signature: public fun String.toLongOrNull(): Long?
    -> Parses the string to an Long number and returns a number or null in case the string is not a valid representation of a number.
    It does not throw any exception.

    package com.javasampleapproach.string2long
    
    fun main(args : Array) {
    	// use method:
    	// -> public fun String.toLongOrNull(): Long?
    	val number: Long? = "123".toLongOrNull()
    	println("number = $number") // number = 123
    	
    	val a: Long? = "12w".toLongOrNull()
    	println("a = $a") // a = null
    }
    

    2. String.toLongOrNull(radix: Int): Long?

    Method signature: public fun String.toLongOrNull(radix: Int): Long?
    -> Parses the string to an Long number and returns null in-case the string is not a valid representation of a number.
    And IllegalArgumentException will be thrown when radix is not a valid radix for string to number conversion.

    package com.javasampleapproach.string2long
    
    fun main(args : Array<String>) {
    	// use method:
    	// -> public fun String.toLongOrNull(): Long?
    	val number: Long? = "101".toLongOrNull(2)
    	println("number = $number") // number = 5
    	
    	val a: Long? = "12w".toLongOrNull(2)
    	println("a = $a") // a = null
    	
    	val b: Long? = "192".toLongOrNull(8)
    	println("b = $b") // b = null
    	
    	try{
    		"101".toLongOrNull(-1)
    	}catch(e: IllegalArgumentException){
    		println(e.printStackTrace())
    		// -> print on console
    		/*
    			java.lang.IllegalArgumentException: radix -1 was not in valid range 2..36
    			at kotlin.text.CharsKt__CharJVMKt.checkRadix(CharJVM.kt:155)
    			at kotlin.text.StringsKt__StringNumberConversionsKt.toLongOrNull(StringNumberConversions.kt:243)
    			at com.javasampleapproach.string2long.ConvertString2LongKt.main(ConvertString2Long.kt:16)
    		*/
    	}
    }
    0 0 votes
    Article Rating
    Subscribe
    Notify of
    guest
    221 Comments
    Oldest
    Newest Most Voted
    Inline Feedbacks
    View all comments