Java can handle large numbers of classes with two high-precision large integersBigIntegerand high-precision floating-point numbersbigdecimal, These two classes are in the Java.math package, and to use them you must refer to the package before the class: import Java.math. biginteger; and import Java.math. bigdecimal; or import java.math.*;
below from several aspects to the biginteger and bigdecima Make a simple comparison:
A. constant
BigInteger: One,zero,ten represents 1,0,10 respectively.
The definition is similar to the following: public static final BigInteger one = valueOf (1);
BigDecimal: There are 8 constants for rounding in addition to the above three constants:round_up,round_down,round_ceiling, Round_floor,round_half_up,
Round_half_down, Round_half_even, Round_unnecessary, please refer to the Java API for the specific rounding mode.
Two. Declaration Assignment
BigInteger: BigInteger bi = new BigInteger(byte[] val)
;
New BigInteger(int signum, byte[] magnitude)
;
New BigInteger(int bitLength, int certainty,Random rnd)
;
New BigInteger(int numBits,Random rnd)
;
New BigInteger(String val)
;
new BigInteger(String val, int radix)
;
Constructors can only accept these types,such as the definition is wrong: BigInteger bi = new BigInteger;
Or: BigInteger bi = biginteger.valueof (100);
The array definition is similar to the base type.
BigDecimal: BigDecimal bd = new BigDecimal (100) OR: BigDecimal BD = bigdecimal.valueof (100);
BigDecimal has more constructors than BigInteger and feels easier to use.
By the way, the scanner class in the Java.util package implements the Nextbiginteger () and Nextbigdecimal () methods, which can be used to read the BigInteger and bigdecimal of the console input. Give an example:
Three. Related functions
Add (), subtract (), pow (), ABS (),multiply()等等这一类就不介绍了,神奇的是probablePrime(int bitLength, Random rnd)
, nextProbablePrime()这一类居然和素数扯得上关系。
BigDecimal has a few more ways of formatting control, which is useful for processing output in a variety of different formats.
Striptraillingzeros (): Remove all 0 without affecting the size of the value;
1.50->1.5;
1.00->1;
This function is very useful.
We all know that Java class is generally to take the method of ToString, BigDecimal there are tostring,toplainstring and toengineeringstring three kinds of methods to represent the string,
Here are the features of each of the three different methods:
ToString: using scientific notation If an exponent is needed;
Toengineeringstring:using engineering notation If an exponent is needed.
Toplainstring:without an exponent field.
Java large number Class introduction