0

In Scala 2.12.7, how can I create a BigInt from an integer initially longer than a Long? I'm looking for something simple where I write an expression that when evaluated is my original long integer expressed as a BigInt.

For example: (arbitrary 75-digit integer) --> equivalentBigInt

that can be accomplished in the REPL.

dogwood
  • 371
  • 2
  • 11

1 Answers1

1

if your input is String, you can use BigInt.apply, which actually uses java.math.BigInteger constructor.

scala> val encodedInt = List.fill(75)("1").mkString("")
encodedInt: String = 111111111111111111111111111111111111111111111111111111111111111111111111111

scala> BigInt(encodedInt)
res0: scala.math.BigInt = 111111111111111111111111111111111111111111111111111111111111111111111111111

using java, which scala.math.BigInt depends on.

scala> import java.math.BigInteger
import java.math.BigInteger

scala> new BigInteger(encodedInt)
res1: java.math.BigInteger = 111111111111111111111111111111111111111111111111111111111111111111111111111
prayagupa
  • 30,204
  • 14
  • 155
  • 192