When we are dealing with very large data, the usual data types are not enough to indicate that in Java there are two classes BigInteger and BigDecimal representing the large integer class and the large floating-point number class, the two classes in theory as long as the computer memory is large enough Can represent a large number of wireless. They are all in the java.math.* package and can be viewed in the API documentation:
Java API 1.6 Chinese online help document http://www.yq1012.com/api/
Instance:
1 ImportJava.math.BigDecimal;2 ImportJava.math.BigInteger;3 4 /**5 * Test Large number6 */7 Public classBignumberdemo {8 9 /**Ten * Test BigInteger Large integer class One */ A Private Static voidTestbiginteger () { -BigInteger bigInt1 =NewBigInteger ("12345678912345678912345");//Instantiate a BigInteger - Longln = 1234567899876543210L; theBigInteger BigInt2 = biginteger.valueof (LN);//returns a BigInteger whose value equals the value of the specified long -System.out.println ("Test BigInteger class:"); -System.out.println ("bigInt1 =" + bigint1.tostring ());//Output Value -System.out.println ("BigInt2 =" +bigint2.tostring ()); + //addition Operation -BigInteger sum = Bigint1.add (BigInt2);//The return value is (BIGINT1+BIGINT2) of the BigInteger +System.out.println ("bigInt1 + BigInt2 =" +sum.tostring ()); A //Subtraction Operations atsum = Bigint1.subtract (BigInt2);//The return value is (BIGINT1-BIGINT2) of the BigInteger -System.out.println ("Bigint1-bigint2 =" +sum.tostring ()); - //Multiplication Operations -sum = bigint1.multiply (BigInt2);//The return value is (BIGINT1*BIGINT2) of the BigInteger -SYSTEM.OUT.PRINTLN ("BigInt1 * BigInt2 =" +sum.tostring ()); - //Division operations: integer quotient insum = Bigint1.divide (BigInt2);//returns a value of (bigint1/bigint2) An integer result of BigInteger -System.out.println ("Bigint1/bigint2 =" +sum.tostring ()); to //Division Operation: Quotient + remainder +Biginteger[] sums = Bigint1.divideandremainder (BigInt2);//returns a value of (bigint1/bigint2) An integer result of BigInteger -System.out.println ("Bigint1/bigint2 =" + sums[0].tostring () the+ "\ t remainder: bigInt1% BigInt2 =" + sums[1].tostring ()); * //take the remainder $sum =Bigint1.mod (bigInt2);Panax NotoginsengSystem.out.println ("bigInt1% BigInt2 =" +sum.tostring ()); - //Index thesum = Bigint1.pow (2); +System.out.println ("bigInt1 ^ 2 =" +sum.tostring ()); A //compare two numbers for equality theSystem.out.println ("Compare two numbers is equal:" +bigint1.equals (BigInt2)); + //Compare the size of a two number -System.out.println ("compare two number of sizes:" +Bigint1.compareto (BigInt2) $+ "(if less than-1, equals 0, greater than 1)"); $ } - - /** the * Test BigDecimal large floating-point number class - */Wuyi Private Static voidTestbigdecimal () { theBigDecimal BIGDEC1 =NewBigDecimal ("1234512345678912345678912345.123");//Instantiate a BigDecimal -Double db = 123456789.123456D; WuBigDecimal bigDec2 = bigdecimal.valueof (db);//returns a BigDecimal whose value equals the value of the specified double -System.out.println ("\ n Test bigdecimal class:"); AboutSystem.out.println ("BIGDEC1 =" + BIGDEC1);//Output Value $System.out.println ("BIGDEC2 =" +bigdec2.tostring ()); - //addition Operation -BigDecimal sum = Bigdec1.add (BIGDEC2);//The return value is (BIGDEC1+BIGDEC2) of the BigDecimal -System.out.println ("BIGDEC1 + bigDec2 =" +sum.tostring ()); A //Subtraction Operations +sum = Bigdec1.subtract (BIGDEC2);//The return value is (BIGDEC1-BIGDEC2) of the BigDecimal theSystem.out.println ("BIGDEC1-BIGDEC2 =" +sum.tostring ()); - //Multiplication Operations $sum = bigdec1.multiply (BIGDEC2);//The return value is (BIGDEC1*BIGDEC2) of the BigDecimal theSYSTEM.OUT.PRINTLN ("bigDec1 * bigDec2 =" +sum.tostring ()); the //Division operations: integer quotient thesum = Bigdec1.dividetointegralvalue (BIGDEC2);//returns a value of (BIGDEC1/BIGDEC2) An integer result of BigDecimal theSystem.out.println ("BIGDEC1/BIGDEC2 =" +sum.tostring ()); - //Division Operation: Quotient + remainder inBigdecimal[] sums = Bigdec1.divideandremainder (BIGDEC2);//returns a value of (BIGDEC1/BIGDEC2) An integer result of BigDecimal theSystem.out.println ("BIGDEC1/BIGDEC2 =" + sums[0].tostring () the+ "\ t remainder: bigDec1% bigDec2 =" + sums[1].tostring ()); About //take the remainder thesum =Bigdec1.remainder (BIGDEC2); theSystem.out.println ("bigDec1% BIGDEC2 =" +sum.tostring ()); the //Index +sum = Bigdec1.pow (2); -System.out.println ("bigDec1 ^ 2 =" +sum.tostring ()); the //compare two numbers for equalityBayiSystem.out.println ("Compare two numbers is equal:" +bigdec1.equals (BIGDEC2)); the //Compare the size of a two number theSystem.out.println ("compare two number of sizes:" +Bigdec1.compareto (BIGDEC2) -+ "(if less than-1, equals 0, greater than 1)");//Lowest data accuracy -System.out.println ("BigDec1 of the lowest data accuracy:" +Bigdec1.ulp ()); the } the the Public Static voidMain (string[] args) { the Testbiginteger (); - testbigdecimal (); the } the the}
Java large number processing class: BigInteger class and BigDecimal class