How to convert String to Long in Kotlin?

前端 未结 11 1009
旧时难觅i
旧时难觅i 2021-01-31 13:00

So, due to lack of methods like Long.valueOf(String s) I am stuck.

How to convert String to Long in Kotlin?

11条回答
  •  野的像风
    2021-01-31 13:29

    It's interesting. Code like this:

    val num = java.lang.Long.valueOf("2");
    println(num);
    println(num is kotlin.Long);
    

    makes this output:

    2
    true
    

    I guess, Kotlin makes conversion from java.lang.Long and long primitive to kotlin.Long automatically in this case. So, it's solution, but I would be happy to see tool without java.lang package usage.

提交回复
热议问题