If the precision of the basic integer and floating-point types is not sufficient, you can turn to a couple of handy classes in the java.math package: BigInteger and BigDecimal. These are classes for manipulating numbers with an arbitrarily long sequence of digits. The BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the same for floating-point numbers.
Use the static valueOf method to turn an ordinary number into a big number:
BigInteger a = BigInteger.valueOf(100);Unfortunately, you cannot use the familiar mathematical operators such as + and * to combine big numbers. Instead, you must use methods such as add and multiply in the big number classes.
BigInteger c = a.add(b); // c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)
API:java.math.BigInteger 1.1
-
BigInteger add(BigInteger other)
-
BigInteger subtract(BigInteger other)
-
BigInteger multiply(BigInteger other)
-
BigInteger divide(BigInteger other)
-
BigInteger mod(BigInteger other)
return the sum, difference, product, quotient, and remainder of this big integer and other.
-
int compareTo(BigInteger other)
returns 0 if this big integer equals other, a negative result if this big integer is less than other, and a positive result otherwise.
-
static BigInteger valueOf(long x)
returns a big integer whose value equals x.
P.S. 使用大数前,切记import java.math.*;