Reputation: 1041
Why can I not convert the following string into a long? i am trying to do this in scala.
var a = "153978017952566571852"
val b = a.toLong
when I try to convert it I get the NumberFormatException
Upvotes: 10
Views: 32368
Reputation: 215137
Because the number exceeds the limit of Long Integer which goes from -9223372036854775808 to 9223372036854775807, with maximum of 19 digits, while your string contains 21 digits.
You can convert it to Float or Double if you don't have to be exact:
scala> val b = a.toFloat
b: Float = 1.5397802E20
scala> val b = a.toDouble
b: Double = 1.5397801795256658E20
Upvotes: 17