Skip to content
Advertisement

Is it possible to convert the String “1L” to the corresponding Long value?

I am parsing a config file with the following instructions:

JavaScript

The returned value, that is a String, is later converted into the expected type from the calling method. How can I convert the String 1L into a Long type?
I tried:

JavaScript

but it raises NumberFormatException. Is there a way to do it?

Advertisement

Answer

Neither Long.parseLong nor Long.valueOf can directly parse 1L.

A workaround: You can implement a simple stripTrailingL function to remove the trailing L and then parse it to Long.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement