Big Data OperationsBigInteger
The long type in Java is the largest integer type, and how to represent data that is longer than long. In the world of Java, integers longer than long can no longer be called integers, and they are encapsulated as BigInteger objects. In the BigInteger class, Implementing arithmetic is implemented by methods, not by operators.
How to construct the BigInteger class:
BigInteger b = new BigInteger (str);
In the constructor method, an integer is given in the form of a string
Arithmetic code:
public static void Main (string[] args) {
Big Data encapsulation for BigInteger objects
BigInteger big1 = new BigInteger ("12345678909876543210");
BigInteger big2 = new BigInteger ("98765432101234567890");
Add to implement the addition operation
BigInteger Bigadd = big1. Add (BIG2);
Subtract implementing the subtraction operation
BigInteger bigsub = big1. Subtract (BIG2);
Multiply implementing multiplication operations
BigInteger Bigmul = big1. Multiply (BIG2);
Divide implementing Division operations
BigInteger Bigdiv = Big2. Divide (BIG1);
}
BigDecimal
What happens when you execute the following code in your program?
System. Out.println (0.09 + 0.01);
System. out.println (1.0-0.32);
System. out.println (1.015 *);
System. out.println (1.301/100);
double and float types are prone to loss of precision in operations, resulting in inaccurate data, and Java provides our BigDecimal class with high-precision operations that enable floating-point data
The construction method is as follows:
BigDecimal B = new BigDecimal (str);
It is recommended that floating-point data be given as a string because the parameter results are predictable
Implement the addition subtraction multiplication code as follows: ( the Operation method is consistent with BigInteger )
public static void Main (string[] args) {
Big Data encapsulation for BigDecimal objects
BigDecimal big1 = new BigDecimal ("0.09");
BigDecimal big2 = new BigDecimal ("0.01");
Add to implement the addition operation
BigDecimal Bigadd = Big1.add (BIG2);
BigDecimal big3 = new BigDecimal ("1.0");
BigDecimal big4 = new BigDecimal ("0.32");
Subtract implementing the subtraction operation
BigDecimal bigsub = big3.subtract (BIG4);
BigDecimal Big5 = new BigDecimal ("1.105");
BigDecimal big6 = new BigDecimal ("100");
Multiply implementing multiplication operations
BigDecimal Bigmul = big5.multiply (BIG6);
For the division operation of floating-point data, unlike integers, there may be infinite repeating decimal , so you need to preserve and select rounding mode for the number of digits you need
BigDecimal B = big1.divide (big2, number of digits after the decimal point, rounding mode)
BigDecimal B = big1.divide. (Big2,2,bigdecimal.round_down)
Big Data operations, Java (BigInteger)