Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert string to a long in scala

Tags:

scala

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

like image 768
Programmerr Avatar asked Dec 19 '16 03:12

Programmerr


1 Answers

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
like image 84
Psidom Avatar answered Oct 15 '22 19:10

Psidom