Java / scala BigInteger paste

I have a problem with the Java BigInteger class: I can't paste a large value into BigInteger For example, suppose I want to assign a BigInteger to this number:

26525285981219105863630848482795

I can't allocate it directly because the compiler considers it an integer:

val bi = 26525285981219105863630848482795 //compile error

But I want it to be a BigInteger Is there any way to paste directly into the source code? If there is no such method, is there a method in scala that makes it easier to use the bigint class? Thank you for your help

Solution

Rtperson's answer is correct from a Java point of view, but in Scala, you can use it with Java math. BigIntegers can do more comparisons

For example:

scala> val a = new BigInteger("26525285981219105863630848482795");
a: java.math.BigInteger = 26525285981219105863630848482795

scala> a + a
:7: error: type mismatch;
found   : java.math.BigInteger
required: String
       a + a

A typical way to standardize a course in scala is to use the factory company object located in the course When you write foo (args) in Scala, convert it to foo Apply (args), where foo is the singleton object - companion object Therefore, in order to find a way to build bigints, you can look at the bigint object in the scala library, especially its application structure

Therefore, there are three ways to build bigint: pass an int, a long or a string for parsing Example:

scala> val x = BigInt(12)
x: BigInt = 12

scala> val y = BigInt(12331453151315353L)
y: BigInt = 12331453151315353

scala> val z = BigInt("12124120474210912741099712094127124112432")
z: BigInt = 12124120474210912741099712094127124112432

scala> x + y * z
res1: BigInt = 149508023628635151923723925873960750738836935643459768508

Note that you can use bigint for natural arithmetic operations, which is impossible for BigInteger!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>