Java (BigIntger, BigDecimal) and javabigdecimal
I. BigInteger
If an integer data exceeds the maximum length of long, the data cannot be loaded. Therefore, you must use the BigInteger class to perform the operation. An integer of any precision that is not changeable. In all operations, BigInteger (such as the basic integer type of Java) is expressed in the form of binary complement ). BigInteger provides the counterparts of all basic integer operators in Java and all related methods of java. lang. Math. BigInteger also provides the following operations: modulo arithmetic, GCD calculation, prime number testing, prime number generation, bit operations, and some other operations.
No. |
Method |
Type |
Description |
1 |
Public BigInteger (String val) |
Structure |
Convert a string to data of the BigInteger type |
2 |
Public BigInteger add (BigInteger val) |
Normal |
Addition |
3 |
Public BigInteger subtract (BigInteger val) |
Normal |
Subtraction |
4 |
Public BigInteger multiply (BigInteger val) |
Normal |
Multiplication |
5 |
Public BigInteger divide (BigInteger val) |
Normal |
Division |
6 |
Public BigInteger max (BigInteger val) |
Normal |
Returns the maximum value of two big numbers. |
7 |
Public BigInteger min (BigInteger val) |
Normal |
Returns the minimum value of two big numbers. |
8 |
Public BigInteger [] divideAndRemainder (BigInteger val) |
Normal |
Division operation. The first element of the array is the division operator, and the second element is the division remainder. |
Ii. Use BigInteger to operate large Integers
Package com. pb. demo1; import java. math. bigInteger; public class BigIntegerDemo {public static void main (String [] args) {BigInteger bi1 = new BigInteger ("123456789"); BigInteger bi2 = new BigInteger ("987654321 "); system. out. println ("addition operation:" + bi2.add (bi1); // addition operation System. out. println ("subtraction operation:" + bi2.subtract (bi1); // subtraction operation System. out. println ("Multiplication operation:" + bi2.multiply (bi1); // Multiplication operation System. out. println ("division operation:" + bi2.divide (bi1); // division operation System. out. println ("maximum number:" + bi2.max (bi1); // obtain the maximum number System. out. println ("Min:" + bi2.min (bi1); // obtain the min number BigInteger result [] = bi2.divideAndRemainder (bi1); // division operation System. out. println ("OPERATOR:" + result [0] + "; remainder:" + result [1]) ;}}
Result:
Addition operation: 1111111110 subtraction operation: 864197532 Multiplication operation: 121932631112635269 division operation: 8 maximum: 987654321 minimum: 123456789 OPERATOR: 8; remainder: 9
3. Use BigDecimal to specify the reserved digits of decimal places. You can directly use float or double for programs that do not require accurate calculation accuracy. However, if you need precise calculation results, you must use the BigDecimal class.
No. |
Method |
Type |
Description |
1 |
Public BigDecimal (double val) |
Structure |
Convert double to BigDecimal |
2 |
Public BigDecimal (int val) |
Structure |
Convert int representation to BigDecimal |
3 |
Public BigDecimal (String val) |
Structure |
Convert string representation to BigDecimal |
4 |
Public BigDecimal add (BigDecimal augend) |
Normal |
Addition |
5 |
Public BigDecimal subtract (BigDecimal subtrahend) |
Normal |
Subtraction |
6 |
Public BigDecimal multiply (BigDecimal multiplicand) |
Normal |
Multiplication |
7 |
Public BigDecimal divide (BigDecimal divisor) |
Normal |
Division |