Reputation: 299
I am wanting to convert a String to an Int.
The String, in question, is 1585489022235
.
I have to convert to a String, first, which appears to be successful (I am printing out the above).
val id = data.get("id").toString()
println(id)
val toInt = id.toInt()
When I attempt to convert from a String to an Int, this exception is thrown:
Error Message: java.lang.NumberFormatException: For input string: "1585489022235"
Stacktrace:
java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
java.base/java.lang.Integer.parseInt(Integer.java:658)
java.base/java.lang.Integer.parseInt(Integer.java:776)
Many thanks in advance
Upvotes: 1
Views: 1321
Reputation: 164194
The value "1585489022235"
can't be converted to an Int
because it is greater than the maximum value of the data type Int
which you can get by Int.MAX_VALUE
and it is equal to 2147483647
.
What you can do is covert the string to Long
:
val x = id.toLong()
The maximum value of the data type long is 9223372036854775807
, which you can get by Long.MAX_VALUE
.
You can find more here: Basic Types - Numbers
Upvotes: 4