HelloCW
HelloCW

Reputation: 2365

Why can't I convert a string "20L" to Long in Kotlin?

I know I can assign 10L to a long var aa.

Now I hope to convert a string bb="20L" to a long , but I get the error My Error: For input string: "20L" , why?

 val aa=10L 
 val bb="20L"

 try{
     val cc=bb.toLong()
 }catch (e:Exception){
    logError("My Error: "+e.message?:" None")
 }

Added Content

I hope to store a long value to key "AutoRestoreID", which one is correct between android:defaultValue="-1" and android:defaultValue="-1L" ?

And more, I don't know if I can only store a string value to the key "AutoRestoreID" when I use ListPreference, could you tell me?

<ListPreference
   android:key="AutoRestoreID"
   android:defaultValue="-1"              
/>

Upvotes: 2

Views: 551

Answers (1)

Adam Batkin
Adam Batkin

Reputation: 53034

The string "20L" is not a valid number (the string "20" would be). The "L" helps the compiler to determine the type of the numeric literal (otherwise it might be an Int instead of a Long), but that's a compiler (and therefore Kotlin syntax) feature, nothing else.

Upvotes: 9

Related Questions