Java starts from zero 29 (large number operations (Bigintger, BigDecimal)

Source: Internet
Author: User

First, BigInteger

If an integer data exceeds the maximum type length of an integer at the time of the operation, the data cannot be mounted, so the BigInteger class is used at this time. An integer of immutable arbitrary precision. In all operations, BigInteger (such as the basic integer type of Java) are represented in twos complement form. BigInteger provides a counterpart to all Java's basic integer operators and provides all the relevant methods of Java.lang.Math. In addition, BigInteger provides the following operations: modulo arithmetic, GCD calculation, prime number test, prime generation, bit manipulation, and some other operations.
No. Method Type Describe
1 Public BigInteger (String val) Structure Converts a string into a BigInteger type of data
2 Public BigInteger Add (BigInteger val) Ordinary Addition
3 Public BigInteger Subtract (BigInteger val) Ordinary Subtraction
4 Public BigInteger Multiply (BigInteger val) Ordinary Multiplication
5 Public BigInteger Divide (BigInteger val) Ordinary Division
6 Public BigInteger Max (BigInteger val) Ordinary Returns the maximum value in two large numbers
7 Public BigInteger min (BigInteger val) Ordinary Returns the minimum value from two large numbers
8 Public biginteger[] Divideandremainder (BigInteger val) Ordinary Division operation, the first element of the array is the quotient of the division, the second element is the remainder of the division

Twomanipulating large integers with BigInteger
 PackageCom.pb.demo1;ImportJava.math.BigInteger; Public classBigintegerdemo { Public Static voidMain (string[] args) {BigInteger bi1=NewBigInteger ("123456789"); BigInteger Bi2=NewBigInteger ("987654321"); System.out.println ("Addition operation:" + Bi2.add (BI1));//addition OperationSystem.out.println ("Subtraction operation:" + bi2.subtract (BI1));//Subtraction OperationSystem.out.println ("Multiplication operation:" + bi2.multiply (BI1));//multiplication OperationSYSTEM.OUT.PRINTLN ("Division operation:" + bi2.divide (BI1));//Division OperationSYSTEM.OUT.PRINTLN ("Maximum number:" + Bi2.max (BI1));//Find the maximum numberSystem.out.println ("min:" + bi2.min (BI1));//Find the minimum numberBigInteger result[] = Bi2.divideandremainder (BI1);//Division OperationSystem.out.println ("Quotient is:" + result[0] + "; remainder is:" + result[1]); }}

Results:

Addition operation: 1111111110 subtraction Operation: 864197532 multiplication Operation: 121932631112635269 Division operation:8 Maximum number:987654321 Minimum number:123456789 quotient is:8; remainder is: 9
Third,use BigDecimal to specify the number of reserved digits for decimalsA program that does not require any accurate calculation accuracy can be done directly using a float or double, but if you need to calculate the results accurately, you must use the BigDecimal class.
No. Method Type Describe
1 Public BigDecimal (double val) Structure Converts a double representation to BigDecimal
2 Public BigDecimal (int val) Structure Converts an int representation to BigDecimal
3 Public BigDecimal (String val) Structure Converts a string representation to BigDecimal
4 Public BigDecimal Add (BigDecimal augend) Ordinary Addition
5 Public BigDecimal Subtract (BigDecimal subtrahend) Ordinary Subtraction
6 Public BigDecimal Multiply (BigDecimal multiplicand) Ordinary Multiplication
7 Public BigDecimal Divide (BigDecimal divisor) Ordinary Division

Java starts from zero 29 (large number operations (Bigintger, BigDecimal)

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.