The Java.math package provides two large number operation classes: BigInteger (large integer operation Class) BigDecimal (Size operand Class).
- Large integer operation class: BigInteger
BigInteger class Construction method: Public BigInteger (String val)
Common methods: Public BigInteger Add (BigInteger val)
Public BigInteger Subtract (BigInteger val)
Public BigInteger Multiply (BigInteger val)
Public BigInteger Divide (BigInteger val)
Public biginteger[] Divideandremainder (BigInteger val): The first number is quotient the second number is the remainder
Public BigInteger pow (int exponent) returns the BigInteger whose value is (thisexponent). Note that exponent is an integer rather than a BigInteger.
The above code in the actual use of the use is not big, in the work if you encounter mathematical problems remember to find a third-party development package.
- Number of sizes Operation class: BigDecimal
The basic methods and operations are the same as BigInteger, but the bigdecimal extends a very important method:
The round () method in the Math class uses the practice of omitting all decimal points during rounding operations, but this practice is not desirable in practical use. So MATH.ROUCND () is a method of no practical use, which can only be done with the BigDecimal class.
A division operation is provided in BigDecimal: Public BigDecimal Divide (BigDecimal divisor,int scale,int roundingmode)
Divisor: Dividend
Scale: Number of decimal digits reserved
Roundingmode: Rounding Mode
(public static final int round_half_up
public static final int Round_half_down
)
classmymath{ Public Static DoubleRoundDoubleNumintScale ) {BigDecimal Big=NewBigDecimal (num); BigDecimal Res=big.divide (NewBigDecimal (1), scale, bigdecimal.round_half_up);//rounding so the parentheses use the new BigDecimal (1) returnRes.doublevalue (); }} Public classBigdecimaldemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println (Mymath.round (178.1436534, 6)); System.out.println (Mymath.round (178.1436534, 3)); System.out.println (Mymath.round (178.1436534, 0)); }}
View Code
Java Learning notes--Big Data operations class