Unable to convert string to long scala

Why can't I convert the next line to a long one? I am trying to do this in scala.

var a = "153978017952566571852" val b = a.toLong 

when i try to convert it i get a NumberFormatException

+5
source share
1 answer

Since the number exceeds the Long Integer limit, which goes from -9223372036854775808 to 9223372036854775807, the maximum is 19 digits, and your line contains 21 digits.


You can convert it to Float or Double if you don't need to be precise:

 scala> val b = a.toFloat b: Float = 1.5397802E20 scala> val b = a.toDouble b: Double = 1.5397801795256658E20 
+6
source

Source: https://habr.com/ru/post/1261476/


All Articles