In Java, a variety of operations are encapsulated in the BigInteger class, with the exception of the basic add, subtract, multiply, and divide operations, as well as the absolute, inverse, greatest common divisor, and the determination of prime numbers. When you use the BigInteger class, you can instantiate a BigInteger object and automatically invoke the appropriate constructor. The BigInteger class has many constructors, but the most straightforward is that the parameters represent the numbers to be processed in string form;
BigDecimal and BigInteger can achieve large numbers of operations, the difference is that BigDecimal added the concept of decimals. The general float type and double type data can only be used for scientific calculations or engineering calculations, but because of the high number of digital accuracy required in commercial computing, the Java.math.BigDecimal class is used. The BigDecimal class supports fixed-point numbers of any precision that can be used to accurately calculate currency values.
Cases:
1 Public classBigintegerdemo {2 Public Static voidMain (string[] args) {3BigInteger biginstance =NewBigInteger ("4");//instantiate a large number4 //take this big number plus 2 operation5SYSTEM.OUT.PRINTLN ("addition operation:" + biginstance.add (NewBigInteger ("2")));6 //take this big number minus 2 operation7System.out.println ("Subtraction Operation:"8+ Biginstance.subtract (NewBigInteger ("2")));9 //multiply the large number by the 2 operationTenSystem.out.println ("Multiplication Operation:" One+ biginstance.multiply (NewBigInteger ("2"))); A //take the large number divided by 2 operation -SYSTEM.OUT.PRINTLN ("Division Operation:" -+ Biginstance.divide (NewBigInteger ("2"))); the //Divide the large number by the quotient of 3 -System.out.println ("Pick-up:" -+ Biginstance.divideandremainder (NewBigInteger ("3")) [0]); - //Divide the large number by the remainder of 3 +System.out.println ("Take remainder:" -+ Biginstance.divideandremainder (NewBigInteger ("3")) [1]); + //take the big number 2 times ASystem.out.println ("Do 2 square Operations:" + BIGINSTANCE.POW (2)); at //take the opposite number of the large number -System.out.println ("Take the opposite number of actions:" +biginstance.negate ()); - } -}
Operation Result:
Addition Operation: 6
Subtraction Operation: 2
Multiplication Operation: 8
Division Operation: 2
Pick-up: 1
Take remainder: 1
Do 2 square Operations: 16
Take the opposite number operation: 4
Large number processing