Java Learning notes--Big Data operations class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.