[Common Java class libraries] _ large number operations (bigintger, bigdecimal)

Source: Internet
Author: User

[Common Java class libraries] _ large number operations (bigintger, bigdecimal)

Objectives of this chapter:
You can use biginteger to operate large integers.

Big Data operations. Normally, the next integer can only be placed in the long type, but if there is a number as follows:
111111111111111111111111111111111111111111111111111111111111111111111111111111111

It cannot be saved at all. To solve this problem, two large number operation classes are introduced in Java:

Operation INTEGER: biginteger
Operation decimal: bigdecimal

3.1, biginteger

If an integer has exceeded the maximum length of long, the data cannot be loaded. Therefore, you must use the biginteger class to perform the operation.

Import Java. math. biginteger; public class bigintegerdemo01 {public static void main (string ARGs []) {biginteger bi1 = new biginteger ("123456789 "); // declare the biginteger object biginteger bi2 = new biginteger ("987654321"); // declare the biginteger object 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); // obtain the division operation system of the remainder. out. println ("OPERATOR:" + result [0] + "; remainder:" + result [1]) ;}};

3.2. bigdecimal

This class can complete large decimal operations, and can also be used for precise rounding, which is often used in development.

You can directly use float or double for programs that do not require accurate Accuracy Calculation. However, if you need precise calculation results, you must use the bigdecimal class.

Import Java. math. *; Class mymath {public static double add (double D1, double D2) {// perform addition calculation bigdecimal b1 = new bigdecimal (D1); bigdecimal b2 = new bigdecimal (D2 ); return b1.add (B2 ). doublevalue ();} public static double sub (double D1, double D2) {// perform subtraction to calculate bigdecimal b1 = new bigdecimal (D1); bigdecimal b2 = new bigdecimal (D2 ); return b1.subtract (B2 ). doublevalue ();} public static double MUL (double D1, double D2) {// perform multiplication calculation bigdecimal b1 = new bigdecimal (D1); bigdecimal b2 = new bigdecimal (D2 ); return b1.multiply (B2 ). doublevalue ();} public static double Div (double D1, double D2, int Len) {// perform multiplication to calculate bigdecimal b1 = new bigdecimal (D1 ); bigdecimal b2 = new bigdecimal (D2); Return b1.divide (B2, Len, bigdecimal. round_half_up ). doublevalue ();} public static double round (double D, int Len) {// rounding bigdecimal b1 = new bigdecimal (d); bigdecimal b2 = new bigdecimal (1 ); return b1.divide (B2, Len, bigdecimal. round_half_up ). doublevalue () ;}}; public class bigdecimaldemo01 {public static void main (string ARGs []) {system. out. println ("addition operation:" + mymath. round (mymath. add (10.345, 3.333), 1); system. out. println ("subtraction:" + mymath. round (mymath. sub (10.345, 3.333), 3); system. out. println ("multiplication:" + mymath. round (mymath. mul (10.345, 3.333), 2); system. out. println ("division operation:" + mymath. div (10.345, 3.333, 3 ));}};

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.